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


Messages In This Thread
Noobie - IF and Else statement help - by Jedi - Mar-18-2017, 05:55 AM
RE: Noobie - IF and Else statement help - by wavic - Mar-18-2017, 07:21 AM
RE: Noobie - IF and Else statement help - by Jedi - Mar-19-2017, 02:25 AM
RE: Noobie - IF and Else statement help - by wavic - Mar-19-2017, 03:20 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Noobie Python TypeError question zoid 4 7,935 Sep-01-2021, 02:28 PM
Last Post: zoid
  Noobie Question, Libreries on Intellij donjon 2 3,090 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