开发者

How can I get input and raw_imput functions to work in Python?

开发者 https://www.devze.com 2023-02-23 14:20 出处:网络
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:

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 string
  • raw_input() does not exist

To get similar behaviour in Python 3 as Python 2's input(), use eval(input()).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号