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 If, Else and Elif

Isabella Elyse Letson Ettin
Isabella Elyse Letson Ettin
513 Points

Only else statement is printing, even when I use different names.

Hi! Even though I believe I have indented properly and have compared my code to Craig's, I'm still only getting my "else" statement. This happens even when I input first_name objects that should print other statements. What might be wrong?

Isabella Elyse Letson Ettin
Isabella Elyse Letson Ettin
513 Points

Whoopsie didn't know how to attach my code! Here it is:

first_name = input("What is your first name?  ") 
print("Hello,", first_name)
if first_name == "Bella":
    print(first_name, "is learning Python")
elif first_name == "Creighton":
    print("is learning law. So is Miles!") 
else: 
       print("You should totally learn Python, {}!".format(first_name))
print("Have a great day {}!".format(first_name))

Then, I am getting back this:

treehouse:~/workspace$ python hello.py                                                                         
  File "/home/treehouse/workspace/hello.py", line 5                                                            
    elif first_name == "Creighton"                                                                             
                                  ^                                                                            
SyntaxError: invalid syntax                                                                                    
treehouse:~/workspace$ python hello.py                                                                         
What is your first name?  Creighton                                                                            
Hello, Creighton                                                                                               
You should totally learn Python, Creighton !                                                                   
Have a great day Creighton !                                                                                   
treehouse:~/workspace$ python hello.py                                                                         
What is your first name?  Bella                                                                                
Hello, Bella                                                                                                   
You should totally learn Python, Bella !                                                                       
Have a great day Bella !                                                                                       
treehouse:~/workspace$                                                                                         

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 67,778 Points

Hey Isabella Elyse Letson Ettin, a clue can be found in your output text.

The text shows “You should totally learn Python, Creighton !”, but the code has:

"You should totally learn Python, {}!"

Notice there isn’t any space between the format placeholder and the exclamation point but a space is being printed in the output.

Perhaps you are entering an extra space during typing responses?

One workaround would be to replace the absolute match with a first_name.startswith() statement.

Post back if you need more help. Good luck!!!

Isabella Elyse Letson Ettin
Isabella Elyse Letson Ettin
513 Points

Hi Chris,

Thank you for your help! This turned out the be the problem!