1 product_list=[ 2 ("iphone",5000), 3 ("Mac Pro",9800), 4 ("Bike",800), 5 ("Watch",10600), 6 ("Coffee",31), 7 ("Alex Python",120), 8 ] 9 10 salary=input("Input your salary:") 11 shopping_list=[] 12 if salary.isdigit(): 13 salary=int(salary) 14 while True: 15 for index,item in enumerate(product_list): 16 print(index,item) 17 user_choice=input("选择要买啥?>>>:") 18 if user_choice.isdigit(): 19 user_choice=int(user_choice) 20 if user_choice<len(product_list) and user_choice>=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(f"\033[31;1m Added {p_item} into shopping cart,your current balance is {salary}.\033[0m") 26 27 else: 28 print(f"\033[41;1m你的余额只剩{salary}啦,还买个毛线\033[0m") 29 30 else: 31 print(f"product code {user_choice} is not exist.") 32 33 elif user_choice==‘q‘: 34 print("---------shopping list------------") 35 for p in shopping_list: 36 print(p) 37 print(f"Your current balance is {salary}.") 38 break 39 else: 40 print("invalid option") 41 else: 42 print("Salary invalid")
购物车
原文:https://www.cnblogs.com/guonf/p/14862034.html