Python Forum
A simple "If...Else" question from a beginner
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
A simple "If...Else" question from a beginner
#1
Hi everyone! I encountered this problem and spent hours on trying to resolve it:

The following does run just fine when you enter "eggs", or "cereal" :the contents after "If" get executed without issue. However, After executing the contents after "If", the contents after "Else" get executed too! That is, the sentence "Sorry our auto system does not support this option yet \nPlease ask your waiter directly" always follow, regardless if you enter "eggs", or "cereal" or others.

How come the contents after "If" and "Else" be executed at the same time? Thank you for any insight!! Please see the attached.

Blush
print("Good morning! What would you like for breakfast? \nYou can have eggs, cereal, fruit or pancakes.")

breakfast = input("Please enter your choice of breakfast from the list: ")

if breakfast == "eggs":
    print("How would you like your eggs? \nYou can have them fried, scrambaled, or boiled.")
    
    eggstype = input ("Please enter your choice of eggs from the list: ")
    if eggstype == "boiled":
        print("How would you like your eggs boiled? \nYou can have them hard or soft.")

        boiledtype=input("Please enter hard or soft: ")
        if boiledtype == "hard":
            print ("Thank you! Your hard boiled eggs will arrive soon.")
        if boiledtype == "soft":
            print ("Thank you! Your soft boiled eggs will arrive soon.")
    else:
        print("Sorry your choice is not available.")
if breakfast=="cereal":
    print("What gype of cereal would you like? \nYou may choose porridge, muesli, corn flakes or rice bubbles.")
    cerealtype = input("Pleaes enter your choice of cereals from the list: ")
    if cerealtype == "porridge":
        print("What type of milk you'd like with your cereal? \nYou may have full cream milk or light white milk.")

        milktype = input("Please enter full cream milk or light white milk:")
        if milktype == "full cream milk":
             print("Thank you and your porridge with full cream milk will arrive shortly.")
        if milktype == "light white milk":
            print("Thank you and your porridge with light white milk will arrive shortly.")
    else:
        print("Sorry your choice is not available.")
if breakfast=="fruit":
    print("What type of fruit would you like? \nYou may choose tropical or European fruit.")
    fruittype = input("Please enter your choice of fruit:")
    if fruittype == "tropical":
        print("What type of tropical fruit would you like? \nYou may choose duriam or pineapple.")

        tropicaltype = input("Please enter duriam or pineapple.")
        if tropicaltype == "duriam":
            print("Thank you and your duriam will arrive shortly.")
        if tropicaltype == "pineapple":
            print("Thank you and your pineapple will arrive shortly.")
    else:
        print("Sorry your choice is not available.")
                
            
else:
    print("Sorry our auto system does not support this option yet \nPlease ask your waiter directly")
print("Enjoy your breakfast")
    
Error:
Good morning! What would you like for breakfast? You can have eggs, cereal, fruit or pancakes. Please enter your choice of breakfast from the list: eggs How would you like your eggs? You can have them fried, scrambaled, or boiled. Please enter your choice of eggs from the list: boiled How would you like your eggs boiled? You can have them hard or soft. Please enter hard or soft: soft Thank you! Your soft boiled eggs will arrive soon. Sorry our auto system does not support this option yet Please ask your waiter directly Enjoy your breakfast

.py   Question.py (Size: 2.42 KB / Downloads: 130)
Reply
#2
if you do not want the else to execute change all other if's except first to elif. the else only works with last if.

print("Good morning! What would you like for breakfast? \nYou can have eggs, cereal, fruit or pancakes.")

breakfast = input("Please enter your choice of breakfast from the list: ")

if breakfast == "eggs":
    print("How would you like your eggs? \nYou can have them fried, scrambaled, or boiled.")

    eggstype = input ("Please enter your choice of eggs from the list: ")
    if eggstype == "boiled":
        print("How would you like your eggs boiled? \nYou can have them hard or soft.")

        boiledtype=input("Please enter hard or soft: ")
        if boiledtype == "hard":
            print ("Thank you! Your hard boiled eggs will arrive soon.")
        if boiledtype == "soft":
            print ("Thank you! Your soft boiled eggs will arrive soon.")
    else:
        print("Sorry your choice is not available.")
elif breakfast=="cereal":
    print("What gype of cereal would you like? \nYou may choose porridge, muesli, corn flakes or rice bubbles.")
    cerealtype = input("Pleaes enter your choice of cereals from the list: ")
    if cerealtype == "porridge":
        print("What type of milk you'd like with your cereal? \nYou may have full cream milk or light white milk.")

        milktype = input("Please enter full cream milk or light white milk:")
        if milktype == "full cream milk":
             print("Thank you and your porridge with full cream milk will arrive shortly.")
        if milktype == "light white milk":
            print("Thank you and your porridge with light white milk will arrive shortly.")
    else:
        print("Sorry your choice is not available.")
elif breakfast=="fruit":
    print("What type of fruit would you like? \nYou may choose tropical or European fruit.")
    fruittype = input("Please enter your choice of fruit:")
    if fruittype == "tropical":
        print("What type of tropical fruit would you like? \nYou may choose duriam or pineapple.")

        tropicaltype = input("Please enter duriam or pineapple.")
        if tropicaltype == "duriam":
            print("Thank you and your duriam will arrive shortly.")
        if tropicaltype == "pineapple":
            print("Thank you and your pineapple will arrive shortly.")
    else:
        print("Sorry your choice is not available.")


else:
    print("Sorry our auto system does not support this option yet \nPlease ask your waiter directly")
print("Enjoy your breakfast")
Output:
λ python play.py Good morning! What would you like for You can have eggs, cereal, fruit or p Please enter your choice of breakfast How would you like your eggs? You can have them fried, scrambaled, Please enter your choice of eggs from How would you like your eggs boiled? You can have them hard or soft. Please enter hard or soft: soft Thank you! Your soft boiled eggs will Enjoy your breakfast
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
(Jul-11-2022, 02:07 AM)menator01 Wrote: if you do not want the else to execute change all other if's except first to elif. the else only works with last if.

Hi mentor!

Thank you so much for your help! I changed the If into elif except the first one and it worked!
Sorry I'm embarrassed to ask what do you mean by "the else only works with last if"? And why the previous coding didn't work as desired? Thank you again! It helped me enormously!
Reply
#4
Take this for example, if the first if is true it will execute. If not nothing will happen, the code moves to the next if.
The 2nd if will execute if it is true. If not the else will execute because, the 2nd if is false. The first if does not have a 2nd option to execute if it is false. Hope that makes sense.

if val: # if value is true execute
    ... do this
if val: # if this values is true
    ... do this
else: # The 2nd if value is not true, so execute this
    ... do this
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#5
(Jul-11-2022, 02:34 AM)menator01 Wrote: Take this for example, if the first if is true it will execute. If not nothing will happen, the code moves to the next if.
The 2nd if will execute if it is true. If not the else will execute because, the 2nd if is false. The first if does not have a 2nd option to execute if it is false. Hope that makes sense.

if val: # if value is true execute
    ... do this
if val: # if this values is true
    ... do this
else: # The 2nd if value is not true, so execute this
    ... do this

Hi Mentor!

Yes thank you for your precious time! It makes perfect sense!

However in this case it seems if the first "if" is true, the last "else" still gets executed:
Error:
Good morning! What would you like for breakfast? You can have eggs, cereal, fruit or pancakes. Please enter your choice of breakfast from the list: eggs How would you like your eggs? You can have them fried, scrambaled, or boiled. Please enter your choice of eggs from the list: boiled How would you like your eggs boiled? You can have them hard or soft. Please enter hard or soft: soft Thank you! Your soft boiled eggs will arrive soon. Sorry our auto system does not support this option yet Please ask your waiter directly Enjoy your breakfast
So if the first if is true, why "else" still gets executed?

I'm sorry to be a pain Blush Blush Blush
Reply
#6
Fruit is not true. No fruit was asked about. In the original code. So fruit is false. breakfast == 'fruit'. The else is with this if statement.
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#7
(Jul-11-2022, 03:23 AM)menator01 Wrote: Fruit is not true. No fruit was asked about. In the original code. So fruit is false. breakfast == 'fruit'. The else is with this if statement.

Thank you so much for taking the time to answer all my shallow questions! I couldn't have figured it out with your help! It's much appreciated!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Very Beginner question on simple variables Harvy 1 165 Apr-12-2024, 12:03 AM
Last Post: deanhystad
  Simple Question - ' defined as "a". ?' Ryan012 10 1,597 May-27-2023, 06:03 PM
Last Post: Ryan012
  Very simple question about filenames and backslashes! garynewport 4 1,925 Jan-17-2023, 05:02 AM
Last Post: deanhystad
  Python Tkinter Simple Multithreading Question AaronCatolico1 5 1,559 Dec-14-2022, 11:35 PM
Last Post: deanhystad
Question Beginner Boolean question [Guessing game] TKB 4 2,292 Mar-22-2022, 05:34 PM
Last Post: deanhystad
  Simple arithmetic question ebolisa 5 2,038 Dec-15-2021, 04:56 PM
Last Post: deanhystad
  Simple code question about lambda and tuples JasPyt 7 3,298 Oct-04-2021, 05:18 PM
Last Post: snippsat
Big Grin question about simple algorithm to my problem jamie_01 1 1,665 Oct-04-2021, 11:55 AM
Last Post: deanhystad
  Beginner question NameError amazing_python 6 2,440 Aug-13-2021, 07:28 AM
Last Post: amazing_python
  Beginner question - storing values cybertron2 4 3,189 Mar-09-2021, 04:21 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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