Python Forum
Noobie - IF and Else statement help
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Noobie - IF and Else statement help
#1
Hi There, 

In a nutshell, I have a simple code for a Menu where the user has options to select different menu items from the selection. Certain menu selections lead to sub-options. I am having difficultly understanding nested IF, Else and elif statements.. I've played around "elif" and "if" statements and have fluked my way to get it working. I need a PRO to elaborate and clearly advise where my mistake was. If possible, lead me to a webpage to understand these concepts better.

In the following code I have colored my comments in blue where I describe my actual problem and this is also where the error is located. 

This is also my first time posting. Please advise If I haven't followed protocol or need to change my post in any way. 

Thank you, 
JEdi
code: 
#Program to run Menu for a restaurant based on series of If, elif and else statements

print("What would like from the menu??")

print("1. Eggs")
print("2. Pancakes")
print("3. Waffles")
print("4. Oatmeal")

UserChoice = int(input("Please select a number between 1 and 4: "))

if (UserChoice == 2):

    MenuItem = "Pancakes"

elif (UserChoice == 3):

    MenuItem = "Waffles"

if (UserChoice == 4):

    MenuItem = "Oatmeal"
    print ("You have selected", MenuItem, "from the Menu")

if (UserChoice == 1):
#Currently the app will return top "else" statement (i.e. Else : print ("Wrong Menu Selection. Please select between 1 and 4")) when the #user presses "4" at menu selection. This is incorrect as "4" is part of the menu selection. 
#Although if I change the above "if" statement (i.e. if (UserChoice == 1):) to "elif" (i.e. elif (UserChoice == 1):). Then the app behaves #accordingly. 
#Can you please explain why this occurs? and shed some light on nested IF/Else/elif statements
MenuItem = "Eggs"

print("What bread would you like with your Eggs?")

print("1. Wheat Toast")
print("2. Sour Dough")
print("3. Rye Toast")
print("4. Pancakes")

BreadChoice = int(input("Please select your bread type"))

if BreadChoice == 1:
    print (" You have selected Wheat Toast with your ", MenuItem)

elif BreadChoice == 2:
    print (" You have selected Sour Dough with your ", MenuItem)

elif BreadChoice == 3:
    print (" You have selected Rye Toast with your ", MenuItem)

elif BreadChoice == 4:
    print (" You have selected Pancakes with your ", MenuItem)

else :
    print ("Wrong selection for", MenuItem,  "Please select between 1 and 4")

elif (UserChoice == 2) or (UserChoice == 3):

    print("What topping would yuo like?")

    print("1. Syrup")
    print("2. Strawberries")
    print("3. Powdered Sugar")

    Topping = int(input("Choose a topping: "))

    if Topping == 1:
        print("You have selected Syrup with ", MenuItem)

    elif Topping == 2:
        print("You have selected Strawberries with ", MenuItem)

    elif Topping == 3:
        print("You have selected Powdered Sugar with ", MenuItem)

    else :
        print(" You have made the wrong Topping Selection")

else :
    print ("Wrong Menu Selection. Please select between 1 and 4")
Reply
#2
It works for me:

print("What would like from the menu??")

print("1. Eggs")
print("2. Pancakes")
print("3. Waffles")
print("4. Oatmeal")

UserChoice = int(input("Please select a number between 1 and 4: "))

if (UserChoice == 1):
    MenuItem = "Eggs"

elif (UserChoice == 2):

    MenuItem = "Pancakes"

elif (UserChoice == 3):

    MenuItem = "Waffles"

elif (UserChoice == 4):

    MenuItem = "Oatmeal"
    print ("You have selected", MenuItem, "from the Menu")
Output:
What would like from the menu?? 1. Eggs 2. Pancakes 3. Waffles 4. Oatmeal Please select a number between 1 and 4: 4 ('You have selected', 'Oatmeal', 'from the Menu')
But this is lot of typing.

menu = {'1': "Eggs", '2': "Pancakes", '3': "Waffles", '4': "Oatmeal"}

print("What would like from the menu??")

for n in range(1, 5):
    print("{}. {}".format(n, menu[str(n)]))

UserChoice = input("Please select a number between 1 and 4: ")

try:
    print("You have selected {} from the Menu".format(menu[UserChoice]))
except:
    print("Not in the menu!")
Output:
What would like from the menu?? 1. Eggs 2. Pancakes 3. Waffles 4. Oatmeal Please select a number between 1 and 4: 5 Not in the menu!
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
Thanks for your response Wavic. My apologies  I didnt notice my code being split from the comments.

Please run the full code otherwise the error doesn't occur.

#Program to run Menu for a restaurant based on series of If, elif and else statements

print("What would like from the menu??")

print("1. Eggs")
print("2. Pancakes")
print("3. Waffles")
print("4. Oatmeal")

UserChoice = int(input("Please select a number between 1 and 4: "))

if (UserChoice == 2):

   MenuItem = "Pancakes"

elif (UserChoice == 3):

   MenuItem = "Waffles"

if (UserChoice == 4):

   MenuItem = "Oatmeal"
   print ("You have selected", MenuItem, "from the Menu")

if (UserChoice == 1):

   MenuItem = "Eggs"

   print("What bread would you like with your Eggs?")

   print("1. Wheat Toast")
   print("2. Sour Dough")
   print("3. Rye Toast")
   print("4. Pancakes")

   BreadChoice = int(input("Please select your bread type"))

   if BreadChoice == 1:
       print (" You have selected Wheat Toast with your ", MenuItem)

   elif BreadChoice == 2:
       print (" You have selected Sour Dough with your ", MenuItem)

   elif BreadChoice == 3:
       print (" You have selected Rye Toast with your ", MenuItem)

   elif BreadChoice == 4:
       print (" You have selected Pancakes with your ", MenuItem)

   else :
       print ("Wrong selection for", MenuItem,  "Please select between 1 and 4")

elif (UserChoice == 2) or (UserChoice == 3):

   print("What topping would yuo like?")

   print("1. Syrup")
   print("2. Strawberries")
   print("3. Powdered Sugar")
   
   Topping = int(input("Choose a topping: "))

   if Topping == 1:
       print("You have selected Syrup with ", MenuItem)

   elif Topping == 2:
       print("You have selected Strawberries with ", MenuItem)

   elif Topping == 3:
       print("You have selected Powdered Sugar with ", MenuItem)

   else :
       print(" You have made the wrong Topping Selection")


else : 
   print ("Wrong Menu Selection. Please select between 1 and 4")
Reply
#4
Next time put your code between python tags using ctrl+shift+v or right mouse click>paste as plain text. You can get the python tags by clicking the little python logo from the tools from the post editor here.

This line - elif (UserChoice == 2) or (UserChoice == 3): - after the BreadChoise checking comes after else statement. The order of this conditional statements is if>elif>else. Also, are you sure you have to check for UserChoise not for BreadChoice?
If I change it with if, a MenuItem variable appears out of nothing. If a wrong item is chosen everywhere this variable is not defined at all but you want to print it. The same is for printing the messages for all of the choices before that - BreadChoice.
Output:
What would like from the menu?? 1. Eggs 2. Pancakes 3. Waffles 4. Oatmeal Please select a number between 1 and 4: 5 What bread would you like with your Eggs? 1. Wheat Toast 2. Sour Dough 3. Rye Toast 4. Pancakes Please select your bread type1 Traceback (most recent call last):   File "untitled.py", line 40, in <module>     print (" You have selected Wheat Toast with your ", MenuItem) NameError: name 'MenuItem' is not defined
At the bottom of the code, you have two else statements one after another. 

Put the code in python tags to provide the indentation because the logic in the code is missing. I indented the code just to work and it works.
Do not pay atention on my choices. I don't even know what is all of this.  Cool
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Noobie Python TypeError question zoid 4 7,871 Sep-01-2021, 02:28 PM
Last Post: zoid
  Noobie Question, Libreries on Intellij donjon 2 3,076 Dec-16-2017, 01:59 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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