0

I am trying to learn python and I am following the book 'Automate the boring stuff with python' and I found that when I try to execute the code below using ubuntu's 'python hello.py' (python 2) it will error out but if I use 'python3 hello.py' I have no problems at all.

I want to understand why and if in older python2 I must explicitly 'import' anything at the top of my code for the error to go away? Also if python3 includes a few 'built-in' imported modules I don't need to import anymore where can I find the list of them?

Here is the code:

# this program asks for my name and tells me the lenght of my name

print("Hello world!")
print('What is your name? ')
myName = input()
print('It is good to meet you, ' + myName)
print('The lenght of your name is: ')
print(len(myName))
print('What is your age? ')
myAge = input()
print('You will be ' + str(int(myAge) +1) + ' in a year.')

Here is what I observed:

xubuntu:~/Documents$ python hello.py
Hello world!
What is your name?
Test
Traceback (most recent call last):
  File "hello.py", line 5, in <module>
    myName = input()
  File "<string>", line 1, in <module>
NameError: name 'Giovanni' is not defined
xubuntu:~/Documents$ python3 hello.py
Hello world!
What is your name?
Test
It is good to meet you, Test
The lenght of your name is:
4
What is your age?
22
You will be 22 in a year.

Thanks for helping out a noob :)

0 Answers0