10

In a tutorial, I read that that there is a difference between input and raw_input. I discovered that they changed the behavior of these functions in the Python 3.0. What is the new behavior?

And why in the python console interpreter this

x = input() 

Sends an error but if I put it in a file.py and run it, it does not?

0

3 Answers 3

19

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?

Sign up to request clarification or add additional context in comments.

6 Comments

Whats going on here i tried your lines on the console (3.x) and it interpreted x = input() "hello" <-- i type this on the console when asked) x + " world" y 'x + "world" Why? x is not translated into a string
the error i was getting >>> x = input() Traceback (most recent call last): File "<console>", line 1, in <module> EOFError: EOF when reading a line
@Guillermo, The interpreter section I showed is for 2.x. I messed up on 3.x (i haven't played with it much at all) so I deleted that part of my answer.
Mmm maybe i should start learning python 2.X i just wanted to have the latest with most future learning. but i don't want to get stuck like this as is a such a waste of time, i guess i could find another tutorial.
@Guillermo, that's my final answer. Also, there's nothing that's a waste of time in the processes of learning. It turns out that I did have my understanding of 3.x right, I just tried to do something different in it. Why do you think you're wasting your time?
|
3

input() vs raw_input()

raw_input collects the characters the user types and presents them as a string. input() doesn't just evaluate numbers but rather treats any input as Python code and tries to execute it. Knowledgeable but malicious user could type in a Python command that can even deleted a file. Stick to raw_input() and convert the string into the data type you need using Python's built in conversion functions.

Also input(), is not safe from user errors! It expects a valid Python expression as input; if the input is not syntactically valid, a SyntaxError will be raised.

Comments

3

Its simple:

  1. raw_input() returns string values
  2. while input() return integer values

For Example:

1.

x = raw_input("Enter some value = ") print x 

Output:

Enter some value = 123 '123' 

2.

y = input("Enter some value = ") print y 

Output:

Enter some value = 123 123 

Hence if we perform x + x = It will output as 123123

while if we perform y + y = It will output as 246

1 Comment

Maybe on python 2.x, but not on python 3.x. input() in python 3.x always return a string. Moreover, raw_input() has been deleted from python 3

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.