python简易购物系统 请输入您的购物资金:50 ****************************** 商品列表如下: 1.商品:牛奶,价格:5 2.商品:鸡蛋,价格:20 3.商品:香蕉,价格:10 4.商品:杯子,价格:10
时间: 2023-11-25 12:49:53 浏览: 80
以下是一个简易的Python购物系统,您可以根据您的需求进行修改:
```python
# 商品列表
goods = {
1: {"name": "牛奶", "price": 5},
2: {"name": "鸡蛋", "price": 20},
3: {"name": "香蕉", "price": 10},
4: {"name": "杯子", "price": 10}
}
# 购物车
shop_car = {}
# 输入购物资金
money = int(input("请输入您的购物资金:"))
# 展示商品列表
print("******************************")
for k, v in goods.items():
print(f"{k}.商品:{v['name']},价格:{v['price']}")
# 开始购物
while True:
choice = input("请输入您要购买的商品编号(按q退出):")
if choice == "q":
break
elif int(choice) not in goods.keys():
print("您输入的商品编号不存在,请重新输入!")
continue
else:
k = int(choice)
if money < goods[k]["price"]:
print("您的余额不足,请重新选择!")
continue
else:
money -= goods[k]["price"]
if k in shop_car.keys():
shop_car[k] += 1
else:
shop_car[k] = 1
print(f"您购买的商品是{goods[k]['name']},单价{goods[k]['price']},数量{shop_car[k]}")
# 展示购物车
print("******************************")
print("您的购物车:")
for i, v in enumerate(shop_car.keys()):
print(f"序号:{i+1},商品:{goods[v]['name']},数量:{shop_car[v]}")
# 展示余额
print("******************************")
print(f"您的余额为:{money}")
```
阅读全文