# 定义商品列表 product_list = [ {"name": "手机", "price": 1999}, {"name": "电脑", "price": 4999}, {"name": "耳机", "price": 299}, {"name": "键盘", "price": 399}, ] # 初始化购物列表 shopping_cart = [] # 用户输入购物金额 budget = float(input("请输入您的购物金额:")) # 打印商品列表 print("商品列表:") for index, product in enumerate(product_list): print(f"{index + 1}. {product['name']} - ¥{product['price']}") # 用户输入需要购买的商品 while True: choice = input("请输入您要购买的商品编号(输入'exit'退出): ") if choice.lower() == 'exit': break elif choice.isdigit() and 0 < int(choice) <= len(product_list): selected_product = product_list[int(choice) - 1] if selected_product["price"] <= budget: shopping_cart.append(selected_product) budget -= selected_product["price"] print(f"已将{selected_product['name']}加入购物车,剩余金额: ¥{budget}") else: print("余额不足") else: print("输入无效,请重新输入") # 打印购物清单 print("您的购物清单:") for product in shopping_cart: print(f"{product['name']} - ¥{product['price']}") print(f"剩余金额: ¥{budget}")
ZZMdedie
最新推荐文章于 2024-09-06 21:26:00 发布