Python trace (last last call)

I get an error while executing this code:

nameUser=input("What is your name ? ")    
print (nameUser)

Error message

Traceback (last last call): File "C: /Users/DALY/Desktop/premier.py", line 1, to File "", line 1, to NameError: name 'klj' not defined

What's happening?

+4
source share
2 answers

You are using Python 2, for which the function is input()trying to evaluate the entered expression. As you enter a string, Python treats it as a name and tries to evaluate it. If the variable is not set to this name, you will get an exception NameError.

, Python 2 raw_input(). , , .

: Python 3, input() , raw_input() Python 2.

+3

Python2 input, input() eval(raw_input()). klj, Python , .

raw_input, Python2.

1: klj :

>>> input()
klj
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1, in <module>
NameError: name 'klj' is not defined

2: klj :

>>> klj = 'hi'
>>> input()
klj
'hi'

3: raw_input:

>>> raw_input()
klj
'klj'
+3

Source: https://habr.com/ru/post/1629463/


All Articles