day2 python 购物车程序

利用列表的增删改查及while,for 循环和if语句编写购物程序
1.粗制滥造:(此程序多处写死)

money = int(input("please input money what you have:"))
goods = [
    '1.apple ', '20',
    '2.bbq ', '30',
    '3.cool ', '40',
    '4.duck ', '10']
items = []
countM = 0
while True:
    print(goods)

    item = input("Please select your item(input the item's num;input q exit): ")
    if item == "1":
        items.append(goods[0:2])
        countM += int(goods[1])
        print(goods[0:2], "have have been put into the shopping cart")
    elif item == "2":
        items.append(goods[2:4])
        countM += int( goods[3])
        print(goods[2:4], "have have been put into the shopping cart")
    elif item == "3":
        items.append(goods[4:6])
        countM += int( goods[5])
        print(goods[4:6], "have have been put into the shopping cart")
    elif item == "4":
        items.append(goods[6:8])
        countM += int( goods[7])
        print(goods[6:8], "have have been put into the shopping cart")
    elif item == "q":
        print( "shopping cart list:",items)
        balance = money - countM
        if balance >= 0:
            print("thanks; your balance is",balance)
        else:
            print("your balance is :",balance,"not ennugh!!!")
        break
    else:
        print("please input the item's num")
        continue

2精品程序(动态程序)

money = input("please input money what you have:")
countM = 0

goods = [
    ('apple', 20),
    ('bbq ', 30),
    ('cool ', 40),
    ('duck ', 10),
]
items = []

if money.isdigit():
    money = int(money)
    while True:
        for index, b in enumerate(goods):
            print(index+1,b)
        item = input("Please select your item(input the item's num;input q exit): ")
        if item.isdigit():
            item=int(item)
            if item < len(goods) and item > 0:
                items.append(goods[item-1])
                countM += goods[item-1][1]
                print("your shopping cart list:",items)
            else:
                print("item [%s] is not exist"% item)
        elif item == "q":
            ##print("shopping cart list:"
            balance = money - countM
            if balance >= 0:

                    info = '''
                    --------thanks info-------
                    items:{0}
                    spend: {1}
                    balance: {2}
                    '''.format(items,countM,balance)
                    print(info)
            else:
                print("your balance is :",balance,"not ennugh!!!")
            break
        else:
            print("please input the item's num")
        continue
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值