Python programming: realize self-service ordering applet, including custom menu, ordering, billing, settlement and other functions

[Title] To realize the self-service ordering applet, the following requirements must be met:

       Users can customize menus by inputting dish names and prices, and display the defined menus. Next, the user can order by inputting the name of the dish in the menu. After the user orders, the user submits the order, and the bill after the user's order is displayed. The bill should include the menu (dish name and price) ordered by the user and the final total. price.

【analyze】

     This topic mainly examines the use of basic knowledge of python, including the use of if statements, loop statements, dictionaries, user input, etc. The information is relatively neat.

[programming implementation]

menus = {}
#自定义菜单,用字典存储,输入ok结束
while(True):
    menu = input('please input menus(ex:name,price),finished with ok:')
    if menu!='ok':
        menu = menu.split(",")
        menus[menu[0]] = int(menu[1])
    elif menu.strip() == '':
        print('please input the right menu!')
    else:
        break

width = 40  #设置显示列的宽度为20个字符
print('The menus is:')
print('='*width,'Menus','='*width)  #字符乘以一个数字表示该字符重复几次
print('name'.center(width),'price'.center(width),sep='')
for key in menus.keys():#循环打印出菜单 控制输出的格式,对齐

    print(' '*int(width/2-2),key.ljust(int(width/2+2),' '),str(menus.get(key)).center(width),sep='')

print('='*(width-2),'Menus end','='*(width-2))  #字符乘以一个数字表示该字符重复几次
#用户点餐,输入菜单上的菜名,输入ok停止
print('start to order:')
user_menu = {} #用户点的菜
while(True):
    name = input('please input name,finished with ok:')
    if name != 'ok':
        if name in menus.keys():#菜单中有这个菜
            user_menu[name] = user_menu.get(name,0)+1  #记录菜被点的次数,一个菜可点多个
        else: #不在菜单中则提示
            print('please input the right name in the Menus!')
    else: #输入ok则点餐完成
        break

#显示账单
print('your order is'.center(width*2))
print('name'.ljust(width),'price'.ljust(width),sep='')
print('='*(width*2))  #字符乘以一个数字表示该字符重复几次
total_price = 0
for key in user_menu.keys():#循环打印出菜单 控制输出的格式,对齐
    amount = user_menu.get(key,1)
    price = amount*menus.get(key)
    total_price += price
    print((key+'*'+str(amount)).ljust(width), str(price).ljust(width), sep='')

print('='*(width*2))  #字符乘以一个数字表示该字符重复几次
print('total'.ljust(width),str(total_price).ljust(width),sep='')

 The results show that:

Guess you like

Origin blog.csdn.net/c1007857613/article/details/128218429