Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Basics Types and Branching Comparisons

Begana Choi
PLUS
Begana Choi
Courses Plus Student 13,122 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,122 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.