Python Forum
Python Nested if-else ladders
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Nested if-else ladders
#1
I am trying to utilize a while loop with an if-else ladder nested inside in order to take user input for menu items and add menu item prices together. No matter what I have tried I cannot seem to get the total_cost variable to add the listed prices depending on user input. All I can get it to return as a value is 0.0 (which would be the value I used to initialize the total_cost variable at the beginning).
Here is my code:
#Displaying the menu
def display_menu_of_food_items():
    print(" 1) eggs               $3.25 \n 2) bacon              $4.00 \n 3) pancakes           $2.50 \n 4) orange juice       $1.25 \n 5) oatmeal            $3.99 \n 6) milk               $1.25 \n 7) donut              $2.00")
    return (display_menu_of_food_items)

display_menu_of_food_items()

def get_menu_item():
  menu_item = print (input("Please enter menu line number of diner's desired item "))
  return (menu_item)

get_menu_item()


#Collecting what a customer would like to order for their meal
def get_diner_order():
  total_cost = 0.00
  display_menu_of_food_items()
  while input("Do you want to order an item? Answer y or n") == 'y':
    get_menu_item()
    order = input()
    if (order == 1):
      total_cost = total_cost + 3.25
    elif (order == 2):
      total_cost = total_cost + 4.00
    elif (order == 3):
      total_cost = total_cost + 2.50
    elif (order == 4):
      total_cost = total_cost + 1.25
    elif (order == 5):
      total_cost = total_cost + 3.99
    elif (order == 6):
      total_cost = total_cost + 1.25
    elif (order == 7):
      total_cost = total_cost + 2.00
  else:
    print (float(total_cost))
  return total_cost
get_diner_order()
Any help would be greatly appreciated, I am a beginning coder and have been stuck in this same spot for several days now.
Thank you!
Reply
#2
It looks like you're using Python 3. input() returns a string, and you're comparing it to ints, so they're never going to be equal. I recommend you replace order = input() with order = int(input())
Reply
#3
I see that you are struggeling with the principle of returning values from a function. Take your first function. What does it return? It returns the address where the function is in memory. Not very useful, is it?
def display_menu_of_food_items():
    print(" 1) eggs               $3.25 \n 2) bacon              $4.00 \n 3) pancakes           $2.50 \n 4) orange juice       $1.25 \n 5) oatmeal            $3.99 \n 6) milk               $1.25 \n 7) donut              $2.00")
    return (display_menu_of_food_items)

print(display_menu_of_food_items())
Output:
1) eggs $3.25 2) bacon $4.00 3) pancakes $2.50 4) orange juice $1.25 5) oatmeal $3.99 6) milk $1.25 7) donut $2.00 <function display_menu_of_food_items at 0x7f3fd5ac0f28>
It is better to leave out the return statement in this case. Or else you should return the string and leave te printing to the calling statement. Like this:
def display_menu_of_food_items():
    return(" 1) eggs               $3.25 \n 2) bacon              $4.00 \n 3) pancakes           $2.50 \n 4) orange juice       $1.25 \n 5) oatmeal            $3.99 \n 6) milk               $1.25 \n 7) donut              $2.00")

print(display_menu_of_food_items())
Output:
1) eggs $3.25 2) bacon $4.00 3) pancakes $2.50 4) orange juice $1.25 5) oatmeal $3.99 6) milk $1.25 7) donut $2.00
The same goes for get_menu_item(). What are you returning? The exit status of the print command? And then in the calling function you add an extra "order = input()". I think this is what you really intended:
def get_menu_item():
    menu_item = input("Please enter menu line number of diner's desired item ")
    return int(menu_item)
#...
#...
    order = get_menu_item()
# etc
I hope this helps you.
Reply
#4
Also note that you can use a multiline string to display a menu
menu = """\
 1) eggs               $3.25 
 2) bacon              $4.00 
 3) pancakes           $2.50 
 4) orange juice       $1.25 
 5) oatmeal            $3.99 
 6) milk               $1.25 
 7) donut              $2.00"""

print(menu)
Reply
#5
@micseydel has your answer in that the entry will never match one of the items. I'll add that you could put quotes around each of the numbers in your if statements, then you will be comparing the inputted string against strings, change the inputted string to an int as in micseydel's post, or change the menu to allow text and then compare to "eggs" and "milk", and in all cases you should have a catch for unrecognized entries.
In this case your unrecognized entries are mistaken for no entry so nothing gets added. If you had an else in your elif ladder that posted "I don't recognize that" you would have saved hours of debugging. Just sayin', a good thing to add.
Reply


Forum Jump:

User Panel Messages

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