购物车进阶

Posted aj-aj

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了购物车进阶相关的知识,希望对你有一定的参考价值。

li = [
    {name:苹果,price:10},
    {name:香蕉,price:20},
    {name:西瓜,price:30}
]
car = {}
print(欢迎光临AJ小店)
money = input(请输入您的余额)
if money.isdigit() and int(money) > 0:
    money = int(money)
    while 1:
        for i,k in enumerate(li):
            print(序号{}\t商品{}\t价格{}.format(i+1,k[name],k[price]))
        choose = input(请输入您要选择商品的序号/按q退出)
        if choose.isdigit():
            choose = int(choose)
            if choose > 0 and choose <= len(li):
                num = input(请输入您要购买商品的数量)
                if num.isdigit():
                    num = int(num)
                    if li[choose-1][price] * num <= money:
                        money = money - li[choose - 1][price] * num
                        print(您的余额为{}.format(money))

                        if li[choose-1][name] in car:
                            car[li[choose-1][name]] = car[li[choose-1][name]] + num
                        else:
                            car[li[choose-1][name]] = num
                    else:
                        print(余额不足,请重新输入或输入q退出)
                    continue
                else:
                    print(请重新输入)
                car[choose-1][商品] = li[choose-1]
            else:
                print(请输入正确的序号)
        elif choose == q:
            break
        else:
            print(输入错误,请重新输入)
else:
    print(对不起,请重新输入)
for a,b in car.items():
    print(商品{}\t数量{}.format(a,b))

 

以上是关于购物车进阶的主要内容,如果未能解决你的问题,请参考以下文章