(1) 输入自己所有的钱。
(2) 展示商品的序号,名称及其价格。
(3) 输入要买商品的序号。
(4) 输入要买商品的数量。
(5) 购物车中显示购买的水果名称及其对应的数量和剩余钱。
(6) 如果序号输入有误就提示用户重新输入。
(7) 如果钱不够了提示用户钱不够,并且退出程序。
# 本钱 cart = {} money_input = input("请输入您所拥有的金额:") if money_input.isdigit(): money = int(money_input) if money > 1000: print("土豪你好!请选择水果:") if money < 1: print("不用看了,你娃什么也买不起,走吧!") # 商品展示 product_list = [{'name': '香蕉', 'price': 3}, {'name': '苹果', 'price': 2.5}, {'name': '梨', 'price': 5}, {'name': '橘子', 'price': 4}, {'name': '柚子', 'price': 1.5}] for a, dict in enumerate(product_list): print('序号:{},名称:{},单价:{}'.format(a, dict['name'], dict['price'])) # 选择商品 while 1: index_input = input('请输入序号:'); if index_input.isdigit(): index = int(index_input) if index >= 0 and index <= len(product_list): weight_input = input("请输入重量:") if weight_input.isdigit(): weight = int(weight_input) price = product_list[index - 1]['price'] total_money = price * weight if total_money <= money: product_name = product_list[index - 1]['name'] had = cart.get('product_name') if had: has = cart[product_name] cart = has + weight print(cart) else: cart[product_name] = weight print(cart) money = money - total_money print("找您零钱:", money) else: print("您的余额不足,小老弟!") break else: print("输入重量有误!") else: print("序号超出范围!") else: print("求求你输入正确的序号!") else: print("请输入数字:")