ZZMdedie

# 定义商品列表
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}")
  • 9
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值