python简易购物系统
时间: 2023-07-25 22:43:20 浏览: 55
好的,我可以帮您实现一个简易的Python购物系统,基本流程如下:
1. 定义商品列表和价格
2. 打印商品列表并让用户选择购买的商品
3. 让用户输入购买数量
4. 计算总价并打印出来
5. 让用户确认是否购买,如果确认则减少相应商品的库存
下面是一个简单的实现示例:
```python
# 商品列表和价格
products = {
'apple': 5,
'banana': 3,
'orange': 4,
}
# 打印商品列表
print('商品列表:')
for name, price in products.items():
print(f'{name}: {price}元')
# 用户选择购买的商品和数量
cart = {}
while True:
name = input('请输入要购买的商品名称(按Enter结束):')
if not name:
break
if name not in products:
print('该商品不存在,请重新输入')
continue
quantity = int(input('请输入购买数量:'))
cart[name] = cart.get(name, 0) + quantity
# 计算总价并打印
total_price = sum(products[name] * quantity for name, quantity in cart.items())
print(f'总价:{total_price}元')
# 确认购买并减少库存
confirm = input('是否确认购买?(y/n)')
if confirm.lower() == 'y':
for name, quantity in cart.items():
products[name] -= quantity
print('购买成功!')
else:
print('已取消购买')
```
这是一个简单的实现,您可以根据需要进行修改和扩展。