1 product_list = [
2 ("Iphone",5800),
3 ("Mac pro",9900),
4 ("watch",3300),
5 ("book",100),
6 ("coffee",10),
7 ]
8
9 shopping_list = []
10 salary = input("input your salary:")
11 if salary.isdigit():
12 salary = int(salary)
13 while True:
14 for index,item in enumerate(product_list):
15 #print(product_list.index(item).item)
16 print(index,item)
17 user_choice = input("which your want to buy:")
18 if user_choice.isdigit():
19 user_choice = int(user_choice)
20 if user_choice < len(product_list) and len(product_list)>= 0:
21 p_item = product_list[user_choice]
22 if p_item[1] <= salary: #买得起
23 shopping_list.append(p_item)
24 salary -= p_item[1]
25 print("Added %s into your shopping cart , your current balance is :\033[31;1m%s\033[0m"%(p_item,salary))
26 else:
27 print("\033[32;1m your balance just only %s :\033[0m"%(salary))
28 else:
29 print("\033[31;1mproduct code [%s] is not exist\033[0m"%(user_choice))
30 elif user_choice =="q":
31 print("-----------product_list-----------")
32 for p in shopping_list:
33 print(p)
34 print("your current balance:",salary)
35 exit()
36 else:
37 print("invalid option")