Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
108 views
in Technique[技术] by (71.8m points)

python - Differences between `input` and `raw_input`


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In python 2.x, raw_input() returns a string and input() evaluates the input in the execution context in which it is called

>>> x = input()
"hello"
>>> y = input()
x + " world"
>>> y
'hello world'

In python 3.x, input has been scrapped and the function previously known as raw_input is now input. So you have to manually call compile and than eval if you want the old functionality.

python2.x                    python3.x

raw_input()   --------------> input()               
input()  -------------------> eval(input())     

In 3.x, the above session goes like this

>>> x = eval(input())
'hello'
>>> y = eval(input())
x + ' world'
>>> y
'hello world'
>>> 

So you were probably getting an error at the interpretor because you weren't putting quotes around your input. This is necessary because it's evaluated. Where you getting a name error?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...