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:
#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
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#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 ): |
#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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
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" ) |