0

I have a super basic question, but I'm just starting to learn python. My script:

print('What is your name?')
person = input("Enter name: ")
print("Hello ", person)

is returning an error: NameError: name 'Bob' is not defined.

I have basically just copied and pasted what was from the tutorial at this point, but it still doesn't work unless I put the name in quotation marks. What am I doing wrong?

Racil Hilan
  • 24,690
  • 13
  • 50
  • 55
user3430541
  • 313
  • 1
  • 2
  • 5
  • 6
    Are you sure you are using python3? This looks like python2 where you should use `raw_input`. – Hyperboreus Mar 17 '14 at 20:21
  • 1
    This code just works fine. Can you please copy _the code_ you are using? – Lafexlos Mar 17 '14 at 20:26
  • I swear this is the code verbatim. Also Im writing it in python3, Im running it with python launcher 3.3.5. – user3430541 Mar 17 '14 at 20:37
  • This can't be the full code that you are using, as a `NameError` means that you are trying to access a variable that cannot be accessed in that scope. As you never reference a variable called `Bob`, either that code or that error is false. – anon582847382 Mar 17 '14 at 20:39
  • Try closing your session and re-entering the three lines above in a fresh prompt (assuming you are using an interactive prompt for this). Something must be persisting from a previous attempt. – beroe Mar 17 '14 at 20:43
  • 1
    Make sure, you are running Python 3. Add `#!/usr/bin/env python3` at the very top of the module if you use pylauncher. Add `import sys; print(sys.version)`, to see the version. – jfs Mar 18 '14 at 08:11
  • @user3430541 Are you typing `python yourfile.py` or `python3 yourfile.py`? Or are you just typing `yourfile.py`? – 2rs2ts Mar 18 '14 at 13:40

2 Answers2

0

Your code should work perfectly fine in Python 3. However, in Py2 it will throw a NameError as there are differences between input() and raw_input(). Essentially, input() in Python 2 is the same as eval(raw_input(""Enter name: ")), meaning that it will attempt to run the inputed code as Python.

In Python 3, raw_input() is no more, and input() operates the way you are expecting it to here: Print a line, accept input, and assign it in string format to a variable.

MattDMo
  • 100,794
  • 21
  • 241
  • 231
  • The `print` function is varadic. So OP's last line works fine. – Hyperboreus Mar 17 '14 at 20:22
  • @Hyperboreus you are correct... – MattDMo Mar 17 '14 at 20:23
  • @Hyperboreus changed to reflect actual issue... – MattDMo Mar 17 '14 at 20:30
  • Thanks so much for the reply. It's still not working on my computer, I'm definitely using python 3.3.5 launcher. I am on a mac if that changes anything. But the really interesting part is that if I change it to raw_input() then it works. Ill keep looking into it. Thanks again – user3430541 Mar 17 '14 at 20:43
  • @user3430541 if `raw_input()` works, then somehow you're using Python 2. Try restarting your interpreter, then running `import sys; print(sys.version)` and see what comes up. – MattDMo Mar 17 '14 at 20:48
  • `input()` in Python 2 is `eval(raw_input())`, not `exec`. You could just say that `raw_input()` is renamed to `input()` in Python 3. – jfs Mar 18 '14 at 08:09
0

You simply are not using Python 3. In Python 2, input() works differently; see this excerpt from the docs.

Equivalent to eval(raw_input(prompt)).

So, when you type Bob without quotation marks, you are basically saying eval(Bob). With quotes it is eval("Bob"). raw_input() does not exist in Python 3, so it will not be defined if you are running with Python 3.

Make sure that you are running your file with Python 3:

  • Type which python and which python3 to make sure you have both.
  • Run python and python3 and see which versions you are running when the python shell opens.
  • You can check the version of Python which is running your code by doing import sys; print(sys.version).
  • Run your code via python3 yourfile.py.
  • As J.F. Sebastian notes, you should add #!/usr/bin/env python3 to the very first line of your file. Chances are, you either wrote #! /usr/bin/env python which on OS X is 2.7.x (I think it's 2.7.2, and I think python is a symlink to the python27 binary) and are running your file just by typing its name, or you are running it through python.
2rs2ts
  • 10,662
  • 10
  • 51
  • 95