product_list = [('iphone',5800),('iwatch',2400),('book',80),('apple',50)] #创建列表库shoppinglist = [] #定义购物车的列表salary = input("input your salary:")if salary.isdigit(): #判断输入的salary是否为数字 salary = int(salary) #声明为整型 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: #判断选择的商品编号是否在列表库中 price_item = product_list[user_choice] #单品的价格对应的编号 print(price_item) print(price_item[1]) #单品价格 if price_item[1] <= salary: #单品价格小于用户余额 shoppinglist.append(price_item) #购物车添加对应的商品编号 salary -= price_item[1] #salary 减去购买商品编号的价格 print("added \033[31;1m%s\033[0m into shopping cart, your balance is \033[31;1m%s\033[0m" %(price_item,salary)) #打印出购物车以及余额 else: print("\033[41;1myour current is not enough, %s\033[0m" %salary) #购买的现金不足支付,并打印出余额 对应 if user_choice < len(product_list) and user_choice >=0: else: print("product code [%s] is not exist" % user_choice) #打印出不存在商品列表的 elif user_choice == 'q': #接user_choice 是否是数字,若是q 执行下面 对应 if user_choice.isdigit(): 亦或者 print("---------- shopping list----------") for p in shoppinglist: #循环打印购物车 print(p) print("your current balance :" , salary) #打印当前余额 exit() #退出 else: print("invalid option") # 无效输入 对应 if user_choice.isdigit():
#elif salary is not#print("welcome to buy buy sale")
转载于:https://www.cnblogs.com/qingtianxiaoji/p/7765434.html