Python Forum
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need Assistance
#1
I'm taking an Introductory course and I'm having a really hard time.

I need assistance simulating a check out process at a restaurant

I'm able to complete a function to display the menu but while writing the program that will allow python to display the total is not working

Here is my menu

def menu():
    print("        Restaurant Register        ")
    print()
    print("Dish No.       Dish Name          Price")
    print("--------       ----------         ------")
    print("1              Gang Gai           $10.00")
    print("2              Pad Thai           $ 8.75")
    print("3              Pad Cashew         $ 9.50")
    print("4              Pad Prik           $10.25")
    print("5              Peanut Curry       $ 9.50")
    print("6              Curry Noodle       $11.25")
    print() 
If a user wants to order...one of the options?, How would I display the output in the form of a bill?

Thanks in advance
Reply
#2
Indeed Jack, when you are just printing the items there is not much you can do with it. Before you design your program you should design your data model. Put the dishes and the prices in a list. Then that list will be the center of your program where all the functions work with.
menuitems = [['Gang Gai', 10.00],
             ['Pad Thai',  8.75]]
Then you make a function to display the menu items like you did. Let the customer choose one of the items and then you can easily fetch the price from de menuitems list.
Try it.
Reply
#3
Thanks alot,

I managed to create the list nut I'm having trouble placing it in a function so the user can make a decision anhave the bill calculated with taxes and discounts
Reply
#4
def menu():
    print("        Restaurant Register        ")
    print()
    print("Dish No.       Dish Name          Price")
    print("--------       ----------         ------")
    print("1              Gang Gai           $10.00")
    print("2              Pad Thai           $ 8.75")
    print("3              Pad Cashew         $ 9.50")
    print("4              Pad Prik           $10.25")
    print("5              Peanut Curry       $ 9.50")
    print("6              Curry Noodle       $11.25")
    print()



def bill():
    menu_items = ["1", 10.00, "2", 8.75, "3", 9.50, "4", 10.25, "5", 9.50, "6", 11.25]
    print("            Bill Information            ")
    print("------------------------------------")
    print("Total of all items:                 " + "$")
    print("Taxes:                              ", "+","$",(tax))
    print("                                     --------")
    print("                             Bill:           ")
    



menu_items = ["1", 10.00, "2", 8.75, "3", 9.50, "4", 10.25, "5", 9.50, "6", 11.25]
tax = (.06)
senior = (.10)
gift_card = (3.00)


while True:
    menu()
    selection = input("Enter the item number you want(1-6): ")
    if selection >= "7":
        print("Invalid item choice. Enter valid item number (1-6): ")
        print()
    elif selection == "1": 
        input("Are you 65 years old or older (Y or N)? ")
        if "n":
            input("Would you like to order another item (Y or N)? ")
            if "n":
                input("Do you have a gift card (Y or N)? ")
                bill()
                print()
                break 
This is what I have so far and this is the output when I run it


Restaurant Register

Dish No. Dish Name Price
-------- ---------- ------
1 Gang Gai $10.00
2 Pad Thai $ 8.75
3 Pad Cashew $ 9.50
4 Pad Prik $10.25
5 Peanut Curry $ 9.50
6 Curry Noodle $11.25

Enter the item number you want(1-6): 1
Are you 65 years old or older (Y or N)? n
Would you like to order another item (Y or N)? n
Do you have a gift card (Y or N)? n
Bill Information
------------------------------------
Total of all items: $
Taxes: + $ 0.06
--------
Bill:
Reply
#5
Well done Jack, now we are getting somewhere. It is getting clear what you want. But there is still room for improvement.
You are working with lists and that is good. But now you have 2 lists and you are still printing a fixed menu. When the restaurant would like to add another dish to the menu you would have to add that in 3 places.
Look at this:
def menu(list_of_items):
    print("        Restaurant Register        ")
    print()
    print("Dish No.       Dish Name          Price")
    print("--------       ----------         ------")
    for dish in range(len(list_of_items)):
        print(f"{dish:<15}{list_of_items[dish][0]:<18}${list_of_items[dish][1]:>6.2f}")

menu_items = [['Gang Gai', 10.00],
             ['Pad Thai', 8.75]]

menu(menu_items)
Output:
Restaurant Register Dish No. Dish Name Price -------- ---------- ------ 0 Gang Gai $ 10.00 1 Pad Thai $ 8.75
First you see I defined a two-dimensional list. The list contains dishes and each dish is a list with item 0 being the dish name and item 1 being the price.
Then you see I used this list as a parameter for the menu function. In this case you might also access the list directly from the menu function as the list is in the global scope, but this is bad practice. A function should be self-contained and not rely on secret variables in another scope.

Then it gets clear the customer may order more than one dish. So it is best to have another list with the name "orders". Each chosen dish should be appended to the orders list. And at the end the bill() function should be called with two parameters. Like this:
bill(menu_items, orders)
Can you do that?
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020