写一个校园外卖系统的程序
时间: 2023-09-09 13:08:19 浏览: 24
好的,下面是一个校园外卖系统的程序的简单实现,仅供参考:
```python
# 定义菜单
menu = {
'1': {'name': '炒饭', 'price': 15},
'2': {'name': '盖浇饭', 'price': 18},
'3': {'name': '炸鸡', 'price': 22},
'4': {'name': '鸡腿饭', 'price': 20},
'5': {'name': '米线', 'price': 12}
}
# 定义订单列表
orders = []
# 定义函数:显示菜单
def show_menu():
print('编号\t菜名\t价格')
for k, v in menu.items():
print(f'{k}\t{v["name"]}\t{v["price"]}')
# 定义函数:下单
def place_order():
order = {}
while True:
show_menu()
choice = input('请输入要点的菜品编号(按q键退出):')
if choice == 'q':
break
elif choice in menu:
quantity = int(input('请输入要点的份数:'))
order[menu[choice]['name']] = {'price': menu[choice]['price'], 'quantity': quantity}
else:
print('输入无效,请重新输入!')
orders.append(order)
print('订单已提交!')
# 定义函数:显示订单
def show_orders():
total = 0
for i, order in enumerate(orders):
print(f'订单{i+1}:')
for k, v in order.items():
print(f'{k}\t{v["price"]}\t{v["quantity"]}')
total += v["price"] * v["quantity"]
print(f'总计:{total}元')
# 主程序
while True:
print('请选择操作:')
print('1. 下单')
print('2. 查看订单')
print('3. 退出')
choice = input()
if choice == '1':
place_order()
elif choice == '2':
if orders:
show_orders()
else:
print('当前没有订单!')
elif choice == '3':
break
else:
print('输入无效,请重新输入!')
```
这个程序实现了一个简单的校园外卖系统,用户可以浏览菜单、下单、查看订单等操作。在程序中,菜单和订单都是以字典的形式存储,菜单包括菜品名称和价格,订单包括菜品名称、单价和数量。用户可以通过输入菜品编号和数量来下单,程序会将订单添加到订单列表中,并在订单中显示菜品名称、单价和数量。用户可以选择查看订单,程序会遍历订单列表,并计算出订单的总价。用户可以按q键退出下单操作。