tags:

views:

42

answers:

1

Hi, i'm new to python im self learning but i have a question that i'm afraid it would take years to find a solution for in google and it would be very easy for an expert.

In a tutorial it says that there is a difference between input and raw_input i discovered that they changed the behavior of these functions in the latest version of python(wich i'm learning) 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.

Thank you

A: 

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?

aaronasterling
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
Guillermo Siliceo Trueba
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 Siliceo Trueba
@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.
aaronasterling
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 Siliceo Trueba
@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?
aaronasterling
I mean i don't want to learn something that is on its way of being depreceated like python 2.x. i think i'm just trying to learn the most efficient way. Thanks for your time, i tried the code you used and it worked as it should, the problem with the interpreter was that i'm using the notepad++ python plugin console and it does weird stuff i won't mess with it anymore i'll stick with the default one.
Guillermo Siliceo Trueba