1 Star 0 Fork 0

Python28/yl

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
购物车.py 2.62 KB
一键复制 编辑 原始数据 按行查看 历史
杨礼 提交于 2019-12-16 21:17 . 重命名 购物车3.py 为 购物车.py
goods = [{"name": "电脑", "price": 1999},
{"name": "鼠标", "price": 10},
{"name": "游艇", "price": 20},
{"name": "美女", "price": 998},]
for i in range(len(goods)):
print(i + 1, goods[i]["name"], +goods[i]["price"])
je=input("请输入要充值的金额")
if je.isdigit() and int(je) > 0:
je = int(je)
print("充值成功,当前金额%d" % je)
else:
print("请正确输入")
gwc={}
shopping_money=0
while True:
sp = input("输入选择的商品序号[1-4],N-n(结算)/输入Q或者q退出程序")
if sp.isdigit() and 0 < int(sp) < len(goods) + 1:
print(int(sp), goods[int(sp) - 1]["name"], goods[int(sp) - 1]["price"])
# gwc[int(sp)] = {goods[int(sp) - 1]["name"], goods[int(sp) - 1]["price"]}
# print(gwc)
shoppug_name = goods[int(sp)-1]["name"] # 获取到用户序号对应的商品名字
shoppug_price = goods[int(sp)-1]["price"] #获取到用户序号对应的商品
# print(shoppug_name)
# print(shoppug_price)
if goods[int(sp)-1]["name"] in gwc:
gwc[shoppug_name]["num"] += 1
else:
gwc[shoppug_name] = {"money":shoppug_price,"num":1}
# print(gwc)
elif sp.upper() == "N":
shopping_money = 0
for i in gwc:
print(f"购买的商品:{i} 单价:{gwc[i]['money']} 数量:{gwc[i]['num']}")
shopping_money += gwc[i]['money'] * gwc[i]['num'] #总价
print(shopping_money)
if je - shopping_money >= 0:
je = je - shopping_money
print(f"结算成功 当前剩余金额:{je}")
elif je - shopping_money < 0:
xuanze = input(f"当前余额不足请选择:1: 充值 2: 选择删除商品")
if xuanze == "1":
xuanzecz = input("请输入需要充值的金额")
xuanzecz = int(xuanzecz)
je += xuanzecz
print(f"充值成功当前余额剩余{je}")
elif xuanze == "2":
for i in gwc:
print(f"购买的商品:{i} 单价:{gwc[i]['money']} 数量:{gwc[i]['num']}")
shanchu = input("请输入需要删除的商品")
if shanchu in gwc:
if gwc[shanchu]["num"] == 1:
del gwc[shanchu]
else:
gwc[shanchu]["num"] -= 1
else:
print("请正确输入")
else:
print("请正确输入")
elif sp.upper() == "Q":
break
else:
print("请正确输入")
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/old_boy_education_python_28/yl.git
git@gitee.com:old_boy_education_python_28/yl.git
old_boy_education_python_28
yl
yl
master

搜索帮助