外包 | A Menu-Driven Proram

外包 | A Menu-Driven Proram

1. Question

Write a menu-driven program for De Anza College Food Court. (You need to use functions!)

  • Display the food menu to a user (Just show the 5 options’ names and prices - No need to show the Combos or the details!)
    在这里插入图片描述

  • Ask the user what he/she wants and how many of it. (Check the user inputs) AND use strip() function to strip your inputs(if needed)

  • Keep asking the user until he/she chooses the exit option.

  • Calculate the price.

  • Ask the user whether he/she is a student or a staff. There is no tax for students and a 9% tax for staff. Add the tax price to the total price.

  • Display the bill to the user. The bill includes:

    • The food items
    • The quantities
    • The cost of them
    • The total before tax
    • Tax amount
    • Total price after tax
      在这里插入图片描述
       

2. Code

  1. Burger Menu
    burger_Menu = [
       (5.25, 'De Anza Burger'), (5.75, 'Bacon Cheese'),
       (5.95, 'Mushroom Swiss'), (5.95, 'Western Burger'), (5.95, 'Don Cali Burger')
    ]
    
  2. show_menu()
    def show_menu():
        print('*' * 59)
        print('*%10s%s%10s*' % ('', 'Welcome to De Anza College Food Court', ''))
        print('*' * 59)
        print('%10s%+10s%+20s' % ('', 'Price', 'Item'))
        for i in range(len(burger_Menu)):
            print('%10s%d%+9s%+20s' % ('', i+1, '$'+str(burger_Menu[i][0]), burger_Menu[i][1]))
        print()
    
    在这里插入图片描述
  3. get_input()
    def get_input():
        food_Items = [0 for _ in range(len(burger_Menu))]
        while 1:
            input_Option = input('(6 for exit)Input your Option: ').strip()
            if input_Option not in '123456':
                print('*Excuse Me*: Please Input Option from 1 to 6')
                continue
            else:
                input_Option = int(input_Option)
                if input_Option == 6:
                    print("Thank you, hope to see you again!\n")
                    break
                else:
                    input_Num = input('How many you want: ').strip()
                    if input_Num.isdigit():
                        food_Items[input_Option-1] += int(input_Num)
                    else:
                        print('*Excuse Me*: Please enter an integer')
                        continue
        return food_Items
    
    在这里插入图片描述
  4. compute_bill()
    def compute_bill(food_Items):
        food_bill = [0 for _ in range(len(burger_Menu))]
        for i in range(len(food_Items)):
            food_bill[i] = burger_Menu[i][0] * food_Items[i]
        total_noTax = sum(food_bill)
        is_Student = input('Are you a student?(y/n): ').strip().lower()
        if is_Student == 'y' or is_Student == 'yes':
            tax = 0
        else:
            tax = total_noTax * 0.09
        total_withTax = total_noTax + tax
        return food_bill, total_noTax, tax, total_withTax
    
  5. print_bill()
    def print_bill(food_Items, food_bill, total_noTax, tax, total_withTax):
        print('\n*%20sHere is your bill%20s*' % ('', ''))
        print('%7s%-20s%-16s%-9s%10s' % ('', 'Item', 'Quantities', 'Cost', ''))
        for i in range(len(food_Items)):
            if food_Items[i] != 0:
                print('%7s%-20s%-16s%-9s%10s' % ('', burger_Menu[i][1], food_Items[i], '$'+str(food_bill[i]), ''))
            else:
                continue
        print()
        print('The total before tax: $%.2f' % total_noTax)
        print('Tax amount: $%.2f' % tax)
        print('Total price after tax: $%.2f' % total_withTax)
    
    在这里插入图片描述
  6. main()
    def main():
        show_menu()
        food_Items = get_input()
        if sum(food_Items) != 0:
            food_bill, total_noTax, tax, total_withTax = compute_bill(food_Items)
            print_bill(food_Items, food_bill, total_noTax, tax, total_withTax)
    

3. Sample Run 1

***********************************************************
*          Welcome to De Anza College Food Court          *
***********************************************************
               Price                Item
          1    $5.25      De Anza Burger
          2    $5.75        Bacon Cheese
          3    $5.95      Mushroom Swiss
          4    $5.95      Western Burger
          5    $5.95     Don Cali Burger

(6 for exit)Input your Option: 1
How many you want: 2
(6 for exit)Input your Option: 5
How many you want: 2
(6 for exit)Input your Option: 6
Thank you, hope to see you again!

Are you a student?(y/n): n

*                    Here is your bill                    *
       Item                Quantities      Cost
       De Anza Burger      2               $10.5
       Don Cali Burger     2               $11.9

The total before tax: $22.40
Tax amount: $2.02
Total price after tax: $24.42

Process finished with exit code 0

 

4. Sample Run 2

***********************************************************
*          Welcome to De Anza College Food Court          *
***********************************************************
               Price                Item
          1    $5.25      De Anza Burger
          2    $5.75        Bacon Cheese
          3    $5.95      Mushroom Swiss
          4    $5.95      Western Burger
          5    $5.95     Don Cali Burger

(6 for exit)Input your Option: abc
*Excuse Me*: Please Input Option from 1 to 6
(6 for exit)Input your Option: 9
*Excuse Me*: Please Input Option from 1 to 6
(6 for exit)Input your Option: 1
How many you want: 1.25
*Excuse Me*: Please enter an integer
(6 for exit)Input your Option: 1
How many you want: abc
*Excuse Me*: Please enter an integer
(6 for exit)Input your Option: 6
Thank you, hope to see you again!


Process finished with exit code 0
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值