Why does the input work in Python 3.x and not in 2.7?

3

I ran this same code with only a few minor differences in syntax to fit the right script mode in Python version 2.7.9 and also in Python 3.4.2

Script that I rode in Python 2:

name=input("tell me your name:")
print 'Hello',str(name),'!'
[email protected]:~/Área de trabalho# python teste3.py
tell me your name:joao
Traceback (most recent call last):
  File "teste3.py", line 1, in <module>
    name=input("tell me your name:")
  File "<string>", line 1, in <module>
NameError: name 'joao' is not defined
[email protected]:~/Área de trabalho#

Script that I rode in Python 3:

name=input("tell me your name:")
print("Hello",str(name),"!")
[email protected]:~/Área de trabalho# python3 teste2.py
tell me your name:joao
Hello joao !
[email protected]:~/Área de trabalho#

What is missing in the code I ran in Python 2, to give the string error?

    
asked by anonymous 13.05.2017 / 23:51

1 answer

3

Use raw_input() to read strings in Python 2 :

name = raw_input("tell me your name:")
print 'Hello', str(name), '!'

See running on ideone . And at Coding Ground . Also put it on GitHub for future reference .

In Python 3 they are synonymous, so it works with input() .

    
14.05.2017 / 00:14