python中餐馆订餐的代码
时间: 2023-12-15 20:30:33 浏览: 79
订餐系统源代码
4星 · 用户满意度95%
以下是一个简单的Python餐馆订餐系统的代码示例,包括初始化菜品信息、显示菜品信息、添加菜品到购物车、结算购物车等功能:
```python
# 初始化菜品信息
menu = {
1: {'name': '宫保鸡丁', 'price': 28, 'likes': 0, 'stock': 10},
2: {'name': '鱼香肉丝', 'price': 25, 'likes': 0, 'stock': 8},
3: {'name': '回锅肉', 'price': 30, 'likes': 0, 'stock': 5}
}
# 显示菜品信息
def show_menu():
print('菜单编号\t菜名\t\t单价\t点赞数\t剩余份数')
for k, v in menu.items():
print(f'{k}\t\t{v["name"]}\t{v["price"]}\t{v["likes"]}\t{v["stock"]}')
# 添加菜品到购物车
def add_to_cart(cart, dish_id, count):
if dish_id not in menu:
print('菜品不存在')
return
dish = menu[dish_id]
if dish['stock'] < count:
print('库存不足')
return
cart[dish_id] = cart.get(dish_id, 0) + count
dish['stock'] -= count
print(f'{dish["name"]}已加入购物车')
# 结算购物车
def checkout(cart):
total_price = 0
for dish_id, count in cart.items():
dish = menu[dish_id]
total_price += dish['price'] * count
print(f'总价:{total_price}')
payment = int(input('请输入付款金额:'))
if payment < total_price:
print('付款金额不足')
return
print(f'找零:{payment - total_price}')
for dish_id, count in cart.items():
dish = menu[dish_id]
dish['likes'] += count
cart.clear()
print('结账成功')
# 主程序
def main():
cart = {}
while True:
print('1.查看菜单 2.加入购物车 3.结算购物车 4.退出')
choice = input('请选择操作:')
if choice == '1':
show_menu()
elif choice == '2':
dish_id = int(input('请输入菜品编号:'))
count = int(input('请输入数量:'))
add_to_cart(cart, dish_id, count)
elif choice == '3':
checkout(cart)
elif choice == '4':
break
else:
print('无效操作')
if __name__ == '__main__':
main()
```
阅读全文