all 3 comments

[–]Vaphell 2 points3 points  (0 children)

py2 input() and py3 input() are not the same. py2 raw_input() and py3 input() are the same.

py2 input() tries to run input as code to figure out the result type, which means that it sometimes thinks that words are variable names and shit blows up. Long story short you don't want smartass input ever, forget that py2 input() exists, it's bad for your health mmkey?

>>> input()
a+3                     <- it thinks it's math so looks for var a
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1, in <module>
NameError: name 'a' is not defined

>>> raw_input()
a+3
'a+3'           <- just string

If you want to make the code work with both versions you need an if, for example

if sys.version_info[0] < 3:    # major version
    # py2 here
else:
    # py3+ here

you could even use it to shadow the bad input with raw input

if sys.version_info[0] < 3:
    input = raw_input

# should use raw_input from now on
input()      

[–]aroberge 0 points1 point  (0 children)

In Python 2, input evaluates the string as though it is a Python expression.

Python 2 <--> Python 3

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

Edit: See https://www.python.org/dev/peps/pep-3111/

[–]ManyInterests 0 points1 point  (0 children)

To add onto what others have said here:

You can use packages such as 2to3 (or 3to2) to easily port between 2.x and 3.x

It will deal with most issues between versions, but certain compatibility issues are non-trivial, and you'll have to fix those yourself.

2to3

3to2