1

I get a NameError when I attempt to execute the following code with any possible input string in Python 3.0:

def onePerLine(str):
    for i in str:
        print(i)

word=input("Enter a phrase or word: ")
onePerLine(word)

The error is as follows:

Enter a phrase or word: hello

Traceback (most recent call last):File"C:\Users\R\Documents\Python30\func2.py",line 5, in <module> word=input("Enter a phrase or word: ")

File "<string>", line 1, in <module>

NameError: name 'hello' is not defined 

How do i fix this and get my code to run? PS: I'm a newbie to python and to programming in general. Any assistance would be appreciated.

Ritwik N
  • 21
  • 1
  • 1
  • 2

2 Answers2

3

You are using Python 2, so you need to use raw_input

>>> x = input('')
hello

Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    x = input('')
  File "<string>", line 1, in <module>
NameError: name 'hello' is not defined

Using raw_input

>>> x = raw_input('')
hello
>>> x
'hello'
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
1

You are using python 2 and you need to use raw_input instead of input which evaluate the string and assumes it as a variable name.

input([prompt])

Equivalent to eval(raw_input(prompt)).

...

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

Community
  • 1
  • 1
Mazdak
  • 105,000
  • 18
  • 159
  • 188