0

I am doing the Asking Questions exercise from "Learn Python the Hard Way" 3rd edition by Zed Shaw where my code looks like:

print "How old are you?",

age = raw_input()

print "How tall are you?",

height = raw_input()

print "How much do you weigh?",

weight = raw_input()

print "So, you're %r old, %r tall and %r heavy." % (age, height, weight)

and output should look like:

How old are you? 38

How tall are you? 6'2"

How much do you weigh? 180lbs

So, you're '38' old, '6\'2"' tall and '180lbs' heavy.

However, because I am using Python 3, output initially looks like:

How old are you?
Traceback (most recent call last):
  File "script.py", line 2, in <module>
    age = raw_input()
NameError: name 'raw_input' is not defined

and then like once I replace raw_input() with input():

How old are you?
Traceback (most recent call last):
  File "script.py", line 2, in <module>
    age = input()
EOFError: EOF when reading a line
PeterH
  • 858
  • 1
  • 6
  • 15
ViktorM
  • 135
  • 1
  • 1

2 Answers2

1

It looks like the code you have is python 2 code. Here is a revised version with python 3 syntax.

Here is some reading about the .format() call in the last line. I think the .format method is a lot easier to understand.

And here is some reading for the input() function in python 3

age = input("How old are you?")

height = input("How tall are you?")

weight = input("How much do you weigh?")

print("So, you're {} old, {} tall and {} heavy.".format(age, height, weight))
PeterH
  • 858
  • 1
  • 6
  • 15
  • I do a: print("How old are you?",) age = input("How old are you?") and get: How old are you? How old are you? Traceback (most recent call last): File "script.py", line 2, in age = input("How old are you?") EOFError: EOF when reading a line – ViktorM Oct 18 '17 at 21:45
  • Can you confirm what version of python you are using? Also you don't have to `print("How old are you?")` before the input. The input will automatically print "How old are you?" to the terminal before getting the users input. – PeterH Oct 18 '17 at 21:49
  • Hello, it doesn't matter which version of python he's using, the code you did will work for both python2.xx and python3.xx – OmO Walker Oct 18 '17 at 22:15
  • Not quite true. If you use a `print "something"` in python3 it will give you a `SyntaxError: invalid syntax`. You have to use `print('something')`. That is why I am confused. He stated he was using python3 but is using python 2 print syntax. – PeterH Oct 18 '17 at 22:19
-1

For Python 3 try replacing all of your "raw_input()" with simply "input()" which has replaced "raw_input()"