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

elif syntax error

Hi , while i was testing the code i face the error which says elif first_name == "arsham": SyntaxError = invalid syntax here is my code:

first_name = input("what is your firstname? ") print("hello" , first_name) if first_name == "Craig": print(first_name, "is learning python")
elif first_name == "arsham": print("you should learn python") else: print(first_name , "will start learning python")

4 Answers

Hi Arsham,

I have just started learning Python as well, so I cannot explain why your version is actually not working...

I just know that if you use the .format function it works. And that is what is being used in that course:

So with format it would look like this and worked when I tried:

first_name = input("what is your firstname? ")
print("hello {}".format(first_name))
if first_name == "Craig":
    print("{} is learning python".format(first_name))
elif first_name == "arsham":
    print("you should learn python")
else:
    print("{} will start learning python".format(first_name))

And surely someone else can point out why it does not work to use the concatenation as you did. Because I have no idea why and would be curious to know as well... :-)

Happy coding!

Nils

thanks for all of the replies guys, the problem was in indenting the else. i got it fixed.

Hi Arsham,

Are you sure you quit REPL? Type "exit()" and then try to run your code again.

Robin Versloot
Robin Versloot
7,326 Points

Try to print a variable then put a + and then the string.

Example: Print("welcome to Python" + first_name)