Prompting for user input in Python 3


In Python 3 the raw_input() function was replaced by the input() function.

examples/basics/prompt3.py
def main():
    print("We have a question!")
    name = input('Your name: ')
    print('Hello ' + name + ', how are you?')

main()

What happens if you run this using Python 2 ?


/usr/bin/python2 prompt3.py


We have a question!
Your name: Foo Bar
Your name: Traceback (most recent call last):
  File "prompt3.py", line 5, in <module>
    main()
  File "prompt3.py", line 2, in main
    name = input('Your name: ')
  File "<string>", line 1
    Foo Bar
          ^
SyntaxError: unexpected EOF while parsing


We have a question!
Your name: Foo
Your name: Traceback (most recent call last):
  File "prompt3.py", line 5, in <module>
    main()
  File "prompt3.py", line 2, in main
    name = input('Your name: ')
  File "<string>", line 1, in <module>
NameError: name 'Foo' is not defined


We have a question!
Your name: __import__("os").unlink(__file__) or "Hudini"
Hello Hudini, how are you?