python小商店

(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("请输入数字:")

 

转载于:https://www.cnblogs.com/Jery-9527/p/10615685.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值