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

Robert Rydlewski
Robert Rydlewski
3,828 Points

Problem with running the program

This is my code first_name = input("What's is your first name? ") print("Hello", first_name) if first_name == "Robert": print(first_name, "is learning Python") print("Have a greate day {}!".format(first_name))

When I run the program in terminal and program as for my name, regardless if I put "Robert" name or any other word I getting this error.

ash-3.2$ python hello.py What's is your first name? Robert Traceback (most recent call last): File "hello.py", line 1, in <module> first_name = input("What's is your first name? ") File "<string>", line 1, in <module> NameError: name 'Robert' is not defined bash-3.2$

Any idea ?? I use Visual Studio. I installed Python extension as well as new Python version 3.8

Steven Parker
Steven Parker
231,269 Points

Use Markdown formatting to preserve code appearance, particularly indenting!

3 Answers

Steven Parker
Steven Parker
231,269 Points

Is it possible that the code you are running is different from what you show here?
In particular, could you have left off the quotes around "Robert" in the version you are running?

Robert Rydlewski
Robert Rydlewski
3,828 Points

Hi, Steve, the code is the same but for some reason in Visual Studio when I run the program in a terminal this is what happens: What's is your first name? "Robert" ('Hello', 'Robert') ('Robert', 'is learning Python') Have a great day Robert! but if I put Robert without quotes like the example below this is what happens

What's is your first name? Robert Traceback (most recent call last): File "hello.py", line 1, in <module> first_name = input("What's is your first name? ") File "<string>", line 1, in <module> NameError: name 'Robert' is not defined

My Python code first_name = input("What's is your first name? ") print("Hello", first_name) if first_name == "Robert": print(first_name, "is learning Python") elif first_name == "Maxi": print(first_name, " is learning with fellow students in the Comunity! Me too!") else: print("You should totally learn Python, {}!".format(first_name)) print("Have a greate day {}!".format(first_name))

I am not sure if that's normal or that's not normal behavior?

Steven Parker
Steven Parker
231,269 Points

I'm still not sure what's happening, but here's what I was guessing:

if first_name == "Robert":  # this code is correct
if first_name == Robert:    # but this would cause "NameError: name 'Robert' is not defined"

:point_up: Note: this code has Markdown formatting.

It should not be necessary to use quotes when running the program and answering the prompt.

Robert Rydlewski
Robert Rydlewski
3,828 Points

Well, This is what's happening. When I run the program (" prompt ) in the terminal on "Treehouse Workspace" as well as with "PyCharm" editors. The program runs just fine. ( I don't need to put quotes around the prompt )

However when I run the same program with VisualStudio editor then I have errors "NameError: name 'Robert' is not defined" I guess I will not use Visual Studio for Python as I can't figure it out what's causing that? The only way to run the program in Visual studio terminal I need to put the put with the quotes with the prompt, with apples for me strange and not correct.

This behavior really