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

Caitlin Scouler
Caitlin Scouler
7,082 Points

Error: NameError: name 'Caitlin' is not defined

Hi guys,

Could you please tell me why I am getting this error?

File "ifelif.py", line 1, in <module> first_name = input("What is your first name? ") File "<string>", line 1, in <module> NameError: name 'Caitlin' is not defined

I am using this code:

first_name = input("What is your first name? ") print("Hello,", first_name) if first_name == "Caitlin": print(first_name, "is learning Python") print("Have a great day {}!".format(first_name))

The name of the file is 'ifelif.py'

Many thanks!

6 Answers

Steven Parker
Steven Parker
231,275 Points

When posting code to the forum, use Markdown formatting to preserve the appearance.

But even without formatting, it doesn't seem like this code would produce that error. What exactly do you do to run this program?


UPDATE: So it sounds like you needed to update from version 2? That's good, because versions 2 and 3 (which is used in the courses) are definitely not compatible for several reasons.

Spencer Hurrle
Spencer Hurrle
3,128 Points

As Steven said, try to get familiar with Markdown formatting. I ran the code you provided and it ran just fine. Make sure you have proper indenting inside the if statement, and be aware that if statements are case sensitive. If you are inputing "caitlin" with a lowercase c, the if statement will mismatch and not run.

ifelif.py
first_name = input("What is your first name? ")
print("Hello,", first_name)
if first_name == "Caitlin":
    print(first_name, "is learning Python") 
    print("Have a great day {}!".format(first_name))
Caitlin Scouler
Caitlin Scouler
7,082 Points

Hi guys, thanks for your replies - I may be running Python 2 which could be an issue? Upon reading maybe Python 2 uses 'raw_input' instead of 'input'?

Spencer Hurrle
Spencer Hurrle
3,128 Points

That could be, I'm unfamiliar with Python 2's differences compared to Python 3. Also, ironically, I realized my formatting didn't take the first time and I fixed that. If you want to check out the spacing and formatting, hopefully it helps. Are you running the code inside Team Treehouse's workspace? I've always thought those run on Python 3 as the norm.

Caitlin Scouler
Caitlin Scouler
7,082 Points

Hi Spencer,

I was running it locally, and updating Python (not straightforward on a mac)

It also fixed the 'Try Except' error issue, which was a relief!

Armin Halilovic
Armin Halilovic
3,372 Points

Hi Caitlin,

I had the same issue, I am using VS Code and not the built-in workspace tool and to solve that issue I had to set the default environment to Python3 by pasting in this code at the top of the script:

#!/usr/bin/env python3

#!/usr/bin/env python3

name = input("What is your name: ")
if name == "armin":
    print("Hello {}".format(name))
else:
    print("Hello unknown person")

AFAIK, raw_input() was renamed to just input() in Python3

Pedro Barajas
Pedro Barajas
1,602 Points

Use this code instead:

Instead of using input use raw_input

first_name = raw_input ("Enter your name: ") print("Hello,", first_name)

if first_name == "Pedro": print(first_name + " " + "is learning python") print("Have a great day {}!".format(first_name))

https://stackoverflow.com/questions/21122540/input-error-nameerror-name-is-not-defined