-2
test = input("What's your name")
print(test)

When I run this, it gives me an error if I type in, for example, william

  File "C:\test.py", line 3, in <module>
    test = input("What's your name")
  File "<string>", line 1, in <module>
NameError: name 'william' is not defined

The only thing that works is if I type it with quotes in the interpreter, but that's not how it's supposed to work. What's causing this problem?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
William hart
  • 123
  • 5

1 Answers1

0

In python 2.7, the input function will evaluate what it reads in.

Example:

>>> test = input("What is your name: ")
What is your name: 3 + 3
>>> print(test)
6
>>> type(test)
<type 'int'>

For python 2.7, you should use raw_input which will record whatever it reads in as a string.

Example:

>>> test = raw_input("What is your name: ")
What is your name: William
>>> print(test)
William
>>> type(test)
<type 'str'>
Mark Hannel
  • 767
  • 5
  • 12