How can I get input and raw_imput functions to work in Python?
Whenever I use input and raw_imput in P开发者_Go百科ython, it gives me an error report. Such as in the simple number guessing game. This is what happens:
>>> import random
>>> number= random.randint(1,100)
>>> guess= input("guess a number. ")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'input' is not defined
The same thing happens when I use raw_input. Does anyone know what's going on?
"imput" is not a standard function in Python. Spell it like "input".
You've misspelled input. It should be input and raw_input, not imput and raw_imput.
Note that the meaning and purpose of both input()
and raw_input()
have changed between Python 2 and Python 3.
Python 2
input()
reads keyboard input and parses it as a Python expression (possibly returning a string, number, or something else)raw_input()
reads keyboard input and returns a string (without parsing)
Python 3
input()
reads keyboard input and returns a stringraw_input()
does not exist
To get similar behaviour in Python 3 as Python 2's input()
, use eval(input())
.
精彩评论