#__Author__: "Miny Dong" #__Date__: 2019/2/26 20:36 products_list = [ ('book',100), ('Apple',6000), ('clothes',200), ('watch',2000), ('Mac Pro',8000), ] #打印商品内容 for i,v in enumerate(products_list): print(i,v) salary = int(input("请输入您的薪资总数:")) #购物车 shopping_chart = [] #输入购买编号,循环输入,直到输入命令退出 while True: user_choice = input("请输入商品编号(退出请输入q):") # 验证输入是否合法 if user_choice.isdigit(): user_choice = int(user_choice) if (user_choice < len(products_list)) and (user_choice >= 0): product_name = products_list[user_choice][0] price = products_list[user_choice][1] if price < salary: shopping_chart.append(products_list[user_choice]) salary = salary - price print("%s已经加入到购物车,当前账户余额为%d" % (product_name, salary)) else: print("您的账户余额不足,余额%d元人民币。" % (salary - price)) else: print("----请输入正确的编号(编号范围为0—%s)----" % str(len(products_list)-1)) elif user_choice=="q": print("----------您已购买以下商品----------") # 循环遍历购物车里的商品,购物车存放的是已买商品 for i in shopping_chart: print(i) print("您的余额为:%s" % salary) break else: print("无效的输入")