1

I am new to python and been playing around but I am getting a error on the mac

below is the code I have in the input.py file

person = input('Enter your name: ')
print('Hello', person)

If I run the code this is my output and error

Johnathans-iMac:scripts johnathansmith$ python input.py 
Enter your name: John
Traceback (most recent call last):
  File "input.py", line 1, in <module>
    person = input('Enter your name: ')
  File "<string>", line 1, in <module>
NameError: name 'John' is not defined

Please keep in mind that I am new.. what can it be?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
John
  • 13
  • 1
  • 4
  • This is not a duplicate of that question; that question deals specifically with unexpected evaluation of user input when using `input`; @John is using `raw_input` – Haldean Brown Feb 17 '17 at 21:51

4 Answers4

2

I think you are using Python 2.7. The reason is that with this version of Python, the input function asks for a string from the user, then tries to evaluate that string. Since John is not defined in your code, Python complains.

Try to either run python3, or change to using raw_input instead of input.

With Python 3 (output from interactive session):

> person = input('Enter name:')
Enter name:John

> print('Hey', person)
Hey John

With Python 2:

> person = raw_input('Enter name:')
Enter name:John
> print 'Hey', person
Hey John
Bendik
  • 1,097
  • 1
  • 8
  • 27
  • 2
    @Afflicted From your link: `input([prompt]): Equivalent to eval(raw_input(prompt))`, which is what I tried to explain in the answer. Never said input is for python 3 only. And `raw_input` is _not_ considered worse. In fact, the Python 3 version of `input` is what Python 2 calls `raw_input`. – Bendik Feb 17 '17 at 18:07
2

Change your code to

person = raw_input('Enter your name: ')
print 'Hello', person

In Python2, you need to use raw_input to get user input from the command line. In Python2, print is a statement. Putting the parentheses around the arguments causes a tuple to be created and passed to the print statement.

0

You are running Python 3 code on a Python 2 interpreter. Either upgrade your python with homebrew brew install python3 or use raw_input for python 2

In Python 2, raw_input() returns a string, and input() tries to run the input as a Python expression.

Sarvex
  • 763
  • 2
  • 7
  • 19
  • Thanks I got it working better with the raw_input but the output is still not working 'Enter your name: john ('Hi, ', 'john') Johnathans-iMac:scripts johnathansmith$' – John Feb 17 '17 at 17:56
  • How is it not working? Try putting print('Hello' + ' ' + person) otherwise, you won't get a space between hello and person. update your question with the new output issue, not an additional comment. – suroh Feb 17 '17 at 17:58
  • for the output I am getting "('Hi, ', ' john') – John Feb 17 '17 at 18:01
  • Like I said update your original question with your current output so it's easier to read in the proper format!! – suroh Feb 17 '17 at 18:02
-3
person = input('Enter your name: ')
print('Hello' + ' ' + person)

Enter your name: Aaron
Hello Aaron

Using the same os as the op and this is what I've done, and received as output.

input() # Is NOT a python 3 dependant function as clearly stated in the docs.

https://docs.python.org/2/library/functions.html#input

" input([prompt]) Equivalent to eval(raw_input(prompt)).

This function does not catch user errors. If the input is not syntactically valid, a SyntaxError will be raised. Other exceptions may be raised if there is an error during evaluation.

If the readline module was loaded, then input() will use it to provide elaborate line editing and history features.

Consider using the raw_input() function for general input from users. "

It's neither proper nor improper to use input, in fact, it's typically better the suggestion to use raw_input() comes from old issues that no longer exist, and in this specific situation would not affect the Op.

Furthermore, any syntax errors that exist will occur on the same level while using raw_input there is no valid argument in this situation to use raw_input() over input() lol

Downvoting a correct answer because you're angry that you're wrong is a violation of the rules, it's sad how cancerous and incorrect this site has become in the last few months.

suroh
  • 917
  • 1
  • 10
  • 26