0
print("Welcome! Here is today's menu.") 
print("")

print("- Coffee ($2)") 
print("- Burger ($4)") 
print("- Juice ($3)") 
print("- Muffin ($2)") 
print("- Burrito ($5)") 
print("")

def student_order() : 

    order = input("What would you like to order today?: ")
    print("")

    if (order == 'Coffee' 
        or order == 'Burger' 
        or order == 'Juice' 
        or order == 'Muffin' 
        or order == 'Burrito'):  
    
        order_quantity = input("Quantity: ")
        print("")
        print("Thank you for your order! You ordered " + order_quantity + " " + order + ".")

        coffee_count = 0
        burger_count = 0
        juice_count = 0
        muffin_count = 0
        burrito_count = 0
        order_subtotal = 0

        order_more = input("Anything else? (Yes or No): ")

        while (order_more != 'NO' or order_more != 'no' or order_more != 'No'): 

            if (order_more == 'Yes' or order_more == 'yes' or order_more == 'YES'): 
                student_order()

                if order == ('Coffee'): 
                    coffee_count += order_quantity

                elif order == ('Burger'): 
                    burger_count += order_quantity

                elif order == ('Juice'): 
                    juice_count += order_quantity

                elif order == ('Muffin'): 
                    muffin_count += order_quantity

                elif order == ('Burrito'): 
                    burrito_count += order_quantity

            else: 
                order_subtotal = (coffee_count * 2.00) + (burger_count * 4.00) + (juice_count * 3.00) + (muffin_count * 2.00) + (burrito_count * 5.00)

                print("")
                print("Thank you for your order!")
                print("")            
                print("Your order subtotal is: $", order_subtotal)
                print("")
                order_tax = order_subtotal * 0.13
                print("Your tax is: $", order_tax)
                print("")
                print("Your total is: $", (order_subtotal + order_tax))
                print("")
                print("Please pick up your order at the counter. Have a good day!")
                break 

    else: 
        print("Sorry, " + order + " is not on today's menu. Please choose another item.")
        student_order()

student_order()

Tried initializing food item counts to zero, but doesn't seem to add up correctly. How do I fix this? For example, I add 1 Juice, then add 2 Muffins. The Subtotal should be $7. Right now, it only shows $0 regardless of what quantity you put in. Maybe the order of the code is wrong, but that is just my guess.

  • 2
    You shouldn't call `student_order` recursively. Instead wrap most of the code in a large `while True:` in which you can `continue` with the next iteration where necessary. Each invocation of a function has its own values for local variables although the variables have the same name in the same code. – Michael Butscher Aug 08 '23 at 15:03
  • 2
    Your function isn't `return`-ing anything so recursion is not the way to go here. – not_speshal Aug 08 '23 at 15:11
  • When you call `student_order()` from the main level, you get a fresh invocation of that function, with all of the local variables initialized to their default values. And when you call `student_order()` from inside itself, **the same thing** happens!! You get a fresh invocation of that function. As @MichaelButscher said, you shouldn't use recursion here. Don't call the function from inside itself. – John Gordon Aug 08 '23 at 15:21
  • Take a look at this post. It might help [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – JonSG Aug 08 '23 at 15:25
  • Thank you to everyone!! I finally am getting numbers now, just trying to figure out how to add up the subtotals if the user adds more items. – newcoder Aug 08 '23 at 16:33

1 Answers1

1

As stated by the comments, using a loop here for continual ordering is better than trying to maintain the order state while using recursion. I've written a cleaned version of your code with comments to point to some python/coding concepts that are useful to pickup. Particularly, using a dictionary will save you some lines.

def student_order():
    # Use dictionary to store prices and quantities
    menu = {
        "coffee": [2, 0],
        "burger": [4, 0],
        "juice": [3, 0],
        "muffin": [2, 0],
        "burrito": [5, 0],
    }

    print("Welcome! Here is today's menu.\n")

    # Use string formatting or f-strings for easier string concatenation
    for item in menu:
        print(f"- {item} (${menu[item][0]})")

    order_more = "yes"

    # Use string.lower() or string.upper() to not worry about cases
    while order_more.lower() != "no":
        order = input("What would you like to order today?: ")
        print("")

        if order.lower() in menu:
            order_quantity = input("Quantity: ")
            print(f"\nThank you for your order! You ordered {order_quantity} {order}")
            menu[order][1] += int(order_quantity)

            order_more = input("Anything else? (Yes or No): ")
        else:
            print(f"Sorry, {order} is not on today's menu. Please choose another item.")

    # List comprehension to get an itemized bill
    subtotals = [menu[item][0] * menu[item][1] for item in menu]

    # Sum list to get subtotal
    order_subtotal = sum(subtotals)

    order_tax = 0.13 * order_subtotal

    print("\nThank you for your order!")
    print(f"\nYour order subtotal is: ${order_subtotal}")
    print(f"\nYour tax is: ${order_tax}")
    print(f"\nYour total is: ${order_subtotal + order_tax}")
    print("\nPlease pick up your order at the counter. Have a good day!")
Michael Cao
  • 2,278
  • 1
  • 1
  • 13