''' 需求: 1.启动程序后,让用户输入工资,然后打印商品列表 2.允许用户根据商品编号购买商品 3.用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒 4.可随时退出,推出时,打印已购买商品和余额 ''' commodity = [("苹果手机",13800),("oppo手机",3800),("皮带手机",800),("杂牌手机",300),("坚果手机",2800),("联想手机",6600),("华为手机",8800)] money = 152000 haveBought = [] while True: print("编号 名称 价格") for index,Commodity in enumerate(commodity): print(index + 1, " %s %d" % ( Commodity[0], Commodity[1])) UserInput = input("请输入商品编号:") if UserInput.isdigit(): UserInput = int(UserInput) UserInp = UserInput - 1 if 0 <= UserInp <= (len(commodity)- 1): if money > commodity[UserInp][1]: print(commodity[UserInp]) money -= commodity[UserInp][1] haveBought.append(commodity[UserInp]) print("您刚刚买了 %s 金额为%d 的商品,余额为%d" % (commodity[UserInp][0],commodity[UserInp][1],money)) print("您已经购买了%s"% haveBought ) byesIn = input("任意键继续/N退出") if byesIn == "N": break else: print("您目前的余额不足以购买%s"% (commodity[UserInp][0])) userI = input("您是否要前往充值?请输入Y/N") if userI == 'Y' : break elif userI == 'N' : break else: continue else: print("您输入的商品没有找到 -_-!") continue