# 1.启动程序后,让用户输入工资,然后打印商品列表
# 2.允许用户根据商品编号购买商品
# 3.用户选择商品后,检查余额是否足够,够就直接扣款,不够就提醒
# 4.可随时退出,退出时,打印已购买商品和余额
1 product_list = [ 2 ('iphone', 5800), 3 ('mac pro', 9800), 4 ('bike', 800), 5 ('watch', 10600), 6 ('coffee', 31), 7 ('book', 120), 8 ] 9 shopping_list = [] 10 salary = input("Input your salary:") 11 if salary.isdigit(): 12 salary = int(salary) 13 while True: 14 for index, itme in enumerate(product_list): 15 print(index, itme) 16 user_choice = input("选择要购买的商品:>>>") 17 if user_choice.isdigit(): 18 user_choice = int(user_choice) 19 if len(product_list) > user_choice >= 0: 20 p_itme = product_list[user_choice] 21 if p_itme[1] <= salary: # 钱够用 22 shopping_list.append(p_itme) 23 salary -= p_itme[1] 24 print("Added \033[31;1m%s\033[0m into shopping cart, your current balance is \033[31;1m%s\033[0m" % (p_itme, salary)) 25 else: 26 print("\033[41;1m你的余额只剩[%s]啦,还不买了]\033[0m" % salary) 27 else: 28 print("produt code [%s] is not exist!" % user_choice) 29 30 elif user_choice == 'q': 31 print("--------shopping list--------") 32 for p in shopping_list: 33 print(p) 34 print("Your current balance:", salary) 35 exit() 36 else: 37 print("invalid potion")
标签:salary,itme,product,入门,python,list,choice,user,购物车 From: https://www.cnblogs.com/Remick/p/17016327.html