python基础day7_购物车实例

print("欢迎光临")
money = input("请输入您的金额:")
shopping_car ={}
li = [{"name":"手机","price":2499},
      {"name":"电脑","price":5999},
      {"name":"鼠标垫","price":19},
      {"name":"游艇","price":114900}]
flag = 1
if money.isdigit() and int(money) >0:
    money = int(money)
    while flag:
        for i,k in enumerate(li):
            print("{}		{}		{}".format(i,k["name"],k["price"]))
        choice = input("输入选择的商品号(输入Q或者q,退出程序):")
        if choice.isdigit():
            choice = int(choice)
            if choice >= 0 and choice < len(li):
                # print("对应商品信息:"+li[choice]["name"]+li[choice]["price"])
                num = int(input("请输入要购买的商品数量:"))
                if money > num * li[choice]["price"]:
                    money -= li[choice]["price"] * num
                    if li[choice]["name"] in shopping_car:
                        shopping_car[li[choice]["name"]] += num
                    else:
                        shopping_car[li[choice]["name"]] = num
                else:
                    print("余额不足")
                    break
                print("购物车中商品有{},您的余额是{}".format(shopping_car,money))
            else:
                print("输入有误,重新输入")
        elif choice.upper() == 'Q':break
        else:print("请输入数字!")
原文地址:https://www.cnblogs.com/Cheryol/p/9606651.html