Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialJuliana Vieira
384 PointsCode Bug
Hi, I am new here and also a beginner in Python. I would like to know why else is not printed if the user presses only enter for the input....How do I get the "nevermind then" to show?TIA:)
print("Hello Earthling!")
print("So we meet again!")
earthling_age = int(input("How old are you?"))
my_age = earthling_age + 1000
print("That's not very old. I am",my_age,"years old")
print("I have an important question for you!")
likes_fruit = input("Do you like fruits?")
if likes_fruit == "True":
print("Me too!")
favorite = input("What is your favorite fruit?")
if favorite == "Bananas":
print("Wow! Same for me!")
elif favorite != "Bananas":
print("That's nice. I like Bananas more, though.")
else:
print("Nevermind then!")
1 Answer
Jennifer Nordell
Treehouse TeacherHi there, Juliana Vieira! First, I edited your post to include markdown so that your code looks nice on the forums.
The reason the else
doesn't fire is because there are only two options. Either the user entered "Bananas" or they didn't. In your elif
statement, you said that if what they entered is not "Bananas" then print out "That's nice. I like bananas more, though". You would need a third case.
You might try something like this:
if favorite == "Bananas":
print("Wow! Same for me!")
elif favorite == "Grapes":
print("That's nice. I like Bananas more, though.")
else:
print("Nevermind then!")
If you enter "Bananas", then "Wow! Same for me!" will print. If you enter "Grapes", it will print "That's nice. I like Bananas more, though." If anything else is entered, it will print "Nevermind then!". When you enter a blank line in the terminal, there's still a character being sent along. The character is the newline character or "\n"
. "\n
is not equal to "Bananas" so the print statement for "That's nice. I like Banans more, though." prints.
There is no scenario where anything that is entered is both not equal to "Bananas" and equal to "Bananas"
Hope this helps!
Juliana Vieira
384 PointsThank you, both Jennifer and John. It is clear now. I know it is a silly question but I am really a beginner. Just started learning last week. Thank you for taking the time to help:)
John Sns
4,193 PointsJohn Sns
4,193 PointsBecause, the variable can either be banana or it can't be banana. So, if you give "Bananas", the first if will execute. if you give anything other than like "Apple", the second condition will be true. So, the code will never have to go to the third "else" condition. So, you should try giving different conditions