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

Asked

Viewed 393 times

3

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

Script I ran in Python 2:

name=input("tell me your name:")
print 'Hello',str(name),'!'
root@hikerpath:~/Á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
root@hikerpath:~/Área de trabalho#

Script I ran in Python 3:

name=input("tell me your name:")
print("Hello",str(name),"!")
root@hikerpath:~/Área de trabalho# python3 teste2.py
tell me your name:joao
Hello joao !
root@hikerpath:~/Área de trabalho#

What is missing from the code I have placed in Python 2, to give the error of string?

  • 1

    Another tip, instead of error images, post the error directly as text, read more about this at: https://pt.meta.stackoverflow.com/a/5485/3635 See more ;)

  • @Guilhermenascimento I understood, only in this case, the image is of the result, to prove that it gave error. the code itself is as text. but I’ll be careful with pictures next time.

  • 3

    If you click with the mouse and drag over the terminal you will notice that it is possible to copy the result of the screen, except when it comes to layouts and Guis. I’m writing one of the reasons why images are mostly bad, especially because a lot of people don’t know, but there are visually impaired programmers (believe it or not, they use screen readers and images are interpretable as text for readers). As soon as I finish posting on the subject I send you ;)

  • @Guilhermenascimento Just and Perfect!!! Inclusion is everything! We don’t usually think about this kind of detail!

1 answer

3


  • Thanks! @bigown ! It worked!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.