0

I am trying to learn Python with this book "Automate the boring stuff with Python". However , I am stuck with the first code itself.

I have simple copied this code on my Editor File:

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

In the Shell window I get the first two lines but as soon as I input my name, it gives me an error. I don't know what am I doing wrong.

Python 2.7.8 (default, Jun 30 2014, 16:08:48) [MSC v.1500 64 bit
(AMD64)] on win32 Type "copyright", "credits" or "license()" for more
information.
>>> ================================ RESTART ================================
>>>  Hello world! What is your name? Ashima

Traceback (most recent call last):   File "C:\Users\sahnas01\Desktop\PYTHON\hello.py", line 4, in <module>
myName = input()   File "<string>", line 1, in <module> NameError: name 'Ashima' is not defined
Nelewout
  • 6,281
  • 3
  • 29
  • 39
Sehaj Kaur
  • 25
  • 1
  • 4
  • 2
    Python 2.7.8 is so old it's deprecated... – Guy Jan 14 '20 at 13:38
  • Did you include the chevrons (">") when you pasted? – Neil Jan 14 '20 at 13:39
  • 1
    But the syntax is for python 3, so yeah, first of all install Python 3! Secondly, make sure you are indenting and separating everything correctly. Try online resource and videos which will guide you better (in my opinion) than a book when you starting from 0. – Celius Stingher Jan 14 '20 at 13:39
  • This code works for me, atleast after formatting it to Python format. – Guy Jan 14 '20 at 13:47

1 Answers1

2

First of all you should use Python 3+, because 2 is deprecated. Then look at your code markup when you are asking a question to understand it correctly. This code should work for Python3.

  1. Print Hello World!
  2. Make variable with user input on new line after question
  3. print text + variable from input
  4. print text + length of your var (read about str function and len function)
  5. make variable with your input on new line which contain age
  6. print text + variable with age + text
print('Hello world!')
myName = input('What is your name?\n')
print('It is good to meet you, ' + myName)
print('The length of your name is:' + str(len(myName)))
myAge = input('What is your  age?\n')
print('You will be ' + str(int(myAge) + 1) + ' in a year.')
halfer
  • 19,824
  • 17
  • 99
  • 186
DevMops
  • 101
  • 1
  • 8