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 trial

Python Python Basics Types and Branching Comparisons

Begana Choi
PLUS
Begana Choi
Courses Plus Student 13,126 Points

my 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??

If 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"

Benjamin Boulter We're dealing with when the else statement does run here.

1 Answer

These 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
Begana Choi
Courses Plus Student 13,126 Points

Thank 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?

Begana Choi The age doesn't matter with that print statement. Watch the short video I recorded about this.