python购物车小作业

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

#用户输入账户金额
money=int(input("请输入您的账户金额:"))
#打印商品信息
print("Product List:")
while True:
    for k,v in enumerate(goods,1):
        for i in range(len(goods)):
            if k-1==i:
                print(k,goods[i]["name"],goods[i]["price"])
    #用户选择商品
    products=int(input("请选择你喜欢的商品:"))
    print("你选择的商品是:",goods[products-1]["name"],"花费:",goods[products-1]["price"])
    #计算选择商品后的余额
    money = money-goods[products-1]["price"]
    cur_assets= money
    if cur_assets>0:
        cart["good_list"].append(goods[products-1]["name"])   #如果资金充足,将商品加入购物车,并结算
        cart["price_list"].append(goods[products-1]["price"])
        print("你目前的账户余额为:",cur_assets,"¥")
        choose=input("继续购物,请按'c';结账,请按'o';退出,请按'q'")
        #查看加入购物车的商品信息
        if choose=='q':
            print("谢谢惠顾")
            break;
        elif choose=='c':
                continue
        elif choose=='o':
            total=sum(cart["price_list"])
            if total>money:
                print("余额不足!")
                exit()
            else:
                print("谢谢,本次花费:",total,'¥',"余额:",money-total,"¥")
                break;
        else:
            print("错误,请重新输入:")
    else:
        print("对不起,您的账户余额不足!")
        break

你可能感兴趣的:(python)