1. 程式人生 > >python面試題常考的超市購物車系統

python面試題常考的超市購物車系統

print python面試題 ali ike ren iphone 輸入 car 余額

author = "superman"


product_list = [
(‘Iphone‘,5800),
(‘Mac Pro‘,9800),
(‘Bike‘,800),
(‘Watch‘,10600),
(‘Coffee‘,31),
(‘Alex Python‘,120),
]
shopping_list = []
salary = input("Input your salary:")
if salary.isdigit(): #判斷工資是不是數字
salary = int(salary) #如果是數字就用Int的數據類型
while True: #進入死循環

for index,item in enumerate(product_list): #打印商品列表
#print(product_list.index(item),item)
print(index,item)
user_choice = input("選擇要買嘛?>>>:")
if user_choice.isdigit(): #判斷工資是數字類型
user_choice = int(user_choice)
if user_choice < len(product_list) and user_choice >=0: #判斷商品元素數量

p_item = product_list[user_choice] #通過下標索引把商品取出來
if p_item[1] <= salary: #買的起
shopping_list.append(p_item) #能買得起就把商品添加到列表裏
salary -= p_item[1] #扣錢
print("Added %s into shopping cart,your current balance is \033[31;1m%s\033[0m" %(p_item,salary) )

else:
print("\033[41;1m你的余額只剩[%s]啦,還買個毛線\033[0m" % salary)
else:
print("product code [%s] is not exist!"% user_choice) #輸入的商品序號不存在
elif user_choice == ‘q‘:
print("--------shopping list------")
for p in shopping_list:
print(p)
print("Your current balance:",salary)
exit()
else:
print("invalid option")

python面試題常考的超市購物車系統