python购物车练习

goods = [
    {"name": "电脑", "price": 1999},
    {"name": "鼠标", "price": 10},
    {"name": "游艇", "price": 20},
    {"name": "美女", "price": 998},
]

for k,v in  enumerate(goods,1):
    print(k,v['name'],v['price'])
money = input("请输入你一共有多少钱:").strip()
b=0
dic={}
while True :
    if  money.isdigit() and int(money) > 0:
        a = int(input("请输入你想买的商品序号: ").strip())
        num=int(input("请输入购买的数量: ").strip())
        b+=goods[a-1]['price']*num
        if b<= int(money):
            money=int(money)-b
            print("您购买的东西是: %s" %(goods[a-1]['name']))
            dic['%s' %(goods[a-1]['name']) ] = num
            print("添加购物车成功")
            print("你的购物车:%s" %(dic))
            print(" 请选择是否继续购物,1是继续,0是退出")
            c=input("请输入你的选择: ")
            if int(c)== 0:
                break
            else :
                continue
        else :
            print("您余额不足")
            break
    else :
        print("穷鬼你的钱不够了")
print("你够买的东西有:%s,你还剩余的钱是%s" %(dic,money))

猜你喜欢

转载自www.cnblogs.com/weidaijie/p/9698435.html
今日推荐