goods = [{"name": "小哥哥", "price": 1999}, {"name": "润滑油", "price": 100}, {"name": "情趣玩具", "price": 200}, {"name": "杜蕾斯", "price": 60}, {"name": "冈本", "price": 40}, {"name": "少妇", "price": 500}, {"name": "人妻", "price": 299}, {"name": "学生", "price": 1998}] p_names = [i for i in goods] #创建商品名列表 count = [] #创建计数列表 for i in range(len(goods)): count.append(0) #为计数列表添加元素 print("{2},{0},{1}".format(goods[i]["name"], goods[i]["price"], i + 1)) #打印所有商品名 def buy(): ##确认购买 global balance while 1: check = input("说吧,看上哪个了:") if check.isdigit() and 0 < int(check) < len(goods) + 1: #确认正确输入 count[int(check) - 1] +=1 #为商品个数加一 if balance <= 0: print("别选了,你钱都不够了!!") #余额不足提示 continue else: balance -= goods[int(check) - 1]["price"] #为客户计算余额 elif check.upper() == "Q": #输入Q退出并打印购物车明细及余额 j = 0 for i in count: print("您总共选了{0},{1},{2}个!".format(p_names[j]["name"],p_names[j]["price"],i)) j += 1 print("您的余额为:{}".format(balance)) break else: print("你的输入有误!!") def look(): while 1: demand = input("还有不清楚价格的吗?确认输入Q前往购买:") #查看各商品序号 if demand.isdigit() and 0 < int(demand) < len(goods) + 1: print("{0},{1}".format(goods[int(demand) - 1]["name"], goods[int(demand) - 1]["price"])) #打印该商品明细 elif demand.upper() == "Q": buy() break else: print("我看你是不想嗨了,重新输入!!") while 1: global balance balance = input("兄dei你有多少钱啊?带你去嗨皮!!") if balance.isdigit(): balance = int(balance) look() break else: print("别瞎输入,到底有多少钱??!")