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 trialBegana Choi
Courses Plus Student 13,126 Pointsmy if statement in else statement doesn't work
first_name = input("What is your name? ")
print("hello,", first_name)
if first_name == "begana":
print(first_name, "is learning Python.")
elif first_name == "maximiliane":
print(first_name, "is learning Python with fellow students in the community")
else:
age = int(input("How old are you? "))
if age <= 6:
print("wow, you are {}! if you are confident with your reading already...".format(age))
print("you should totally learn Python, {}!".format(first_name))
print("Have a great day, {}!".format(first_name))
after I give a condition of age, the message about age isn't printed out. can somebody point out what is my mistake??
Adam N
70,280 PointsBenjamin Boulter We're dealing with when the else statement does run here.
1 Answer
Adam N
70,280 PointsThese 2 lines will run if the age is less than or equal to 6:
print("wow, you are {}! if you are confident with your reading already...".format(age))
print("you should totally learn Python, {}!".format(first_name))
if age is greater than 6, only this line will run within the else statement:
print("you should totally learn Python, {}!".format(first_name))
if you want different behavior than this, you'll have to change the indentation. Based on the video, seems like your code is working as it should.
Let me know if this helps!
Begana Choi
Courses Plus Student 13,126 PointsThank you !! :D but I didn't indent this line,
print("you should totally learn Python, {}!".format(first_name))
Why does it print out when the age is less than 6?
Adam N
70,280 PointsBegana Choi The age doesn't matter with that print statement. Watch the short video I recorded about this.
Benjamin Boulter
21,925 PointsBenjamin Boulter
21,925 PointsIf the first name inputted is either "begana" or "maximiliane" then the else statement for the age won't run.
If you want it to run then you need to remove the else statement and put the age variable under the elif or if statement. The else statement only works if the name is neither "begana" or "maximiliane"