欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

-----------python简单购物车小程序-----------

程序员文章站 2022-10-04 17:35:07
2018-07-06 21:06:16 ......
功能要求:
要求用户输入自己拥有总资产,例如:2000
显示商品列表,让用户根据序号选择商品,加入购物车
购买,如果商品总额大于总资产,提示账户余额不足,否则,购买成功。
goods = [
{"name": "电脑", "price": 1999},
{"name": "鼠标", "price": 10},
{"name": "游艇", "price": 20},
{"name": "美女", "price": 998},
]
goods = [
    {"name": "电脑", "price": 1999},
    {"name": "鼠标", "price": 10},
    {"name": "游艇", "price": 20},
    {"name": "美女", "price": 998},
]

a = 0
count = 1
dic = {}
li = []
c = 0
sum = 0
s = int(input("请输入你的资产:"))
while count < 5:
    print("%s  %s  %s" % (count,goods[a]["name"],goods[a]["price"]))
    dic[count] = goods[a]
    count += 1
    a += 1
while 1:
    s1 = input("请你选择要加入购物车的商品(按Q结束选购进行付款):")
    if s1.upper() == "Q":
            break
    elif int(s1) > int(4):
        print("输入有误,请重新输入")
    else:
        li.append(dic[int(s1)]["price"])
        print("%s" % (dic[int(s1)]["price"]))
    c += 1
if int(li[0]) + int(li[c - 1]) > int(s):
    print("户余额不足")
else:
    print("购买成功!")

2018-07-06  21:06:16

“继续奔跑 输掉一切也不要输掉微笑”