products = [['phone1',100],['phone2',200],['phone3',300],['phone4',500],['phone5',500],['phone6',1800]] shopping_cart = [] run_flag = True # 标志位 while run_flag: wage = input("请输入您的工资,退出请输入q|Q:") if wage.isdigit(): wage = int(wage) while run_flag: print("--------商品列表-------") for index,p in enumerate(products): print("%s: %s %s" %(index,p[0],p[1])) choice = input("请输入您购买的商品:") if choice.isdigit(): choice = int(choice) if choice >= 0 and choice < len(products): if wage > products[choice][1]: shopping_cart.append(products[choice]) print("Added product %s into shopping cart. " % (products[choice])) wage -= products[choice][1] print(wage) else: print("您的余额不足") else: print("商品不存在") elif choice == 'q': if len(shopping_cart) > 0: print("---------您购买的产品为-----------") for index, p in enumerate(shopping_cart): print("%s. %s %s" % (index, p[0], p[1])) print('您的余额为:',wage) #break run_flag = False else: print("请输入数字") elif wage == 'q' or wage == 'Q': run_flag = False else: print('工资必须是数字')