0

I've started working on the well known Python for Absolute Beginners 3e. I've been copying the code faithfully, but some how keep getting error messages. So, I used the code provided in the help/examples folder but still get the same message:

Traceback (most recent call last):
  File "word_problems.py", line 6, in <module>
    input("Press the enter key to find out.")
  File "<string>", line 0

    ^
SyntaxError: unexpected EOF while parsing

Can someone give me either a clue, or explain what's not working. I can also post part of the code, the rest is identical (but I guess many people know this book), but with different questions:

print("If a 2000 pound pregnant hippo gives birth to a 100 pound calf,")
print("but then eats 50 pounds of food, how much does she weigh?")
input("Press the enter key to find out.")
print("2000 - 100 + 50 =", 2000 - 100 + 50)
user7409665
  • 19
  • 1
  • 3

3 Answers3

1

useraw_input instead of input

If you use input, then the data you type is is interpreted as a Python Expression which means that you end up with gawd knows what type of object in your target variable, and a heck of a wide range of exceptions that can be generated. So you should NOT use input unless you're putting something in for temporary testing, to be used only by someone who knows a bit about Python expressions.

raw_input always returns a string because, heck, that's what you always type in ... but then you can easily convert it to the specific type you want, and catch the specific exceptions that may occur. Hopefully with that explanation, it's a no-brainer to know which you should use.

source 1 | source 2

Community
  • 1
  • 1
Jesse de gans
  • 1,432
  • 1
  • 14
  • 27
  • 1
    Thanks, I hadn't thought about that! I did a Python course a few years ago, but had forgotten that detail, I just assumed the original code were correct. Thanks. – user7409665 Jan 12 '17 at 16:05
0

You are using python2 version

so you raw_input() instead of input()

and your code should be:

print("If a 2000 pound pregnant hippo gives birth to a 100 pound calf,")
print("but then eats 50 pounds of food, how much does she weigh?")
raw_input("Press the enter key to find out.")
print("2000 - 100 + 50 =", 2000 - 100 + 50)

and If you want show output only on blank input or enter,you can put simple validation as:

print("If a 2000 pound pregnant hippo gives birth to a 100 pound calf,")
print("but then eats 50 pounds of food, how much does she weigh?")
ur = raw_input("Press the enter key to find out.")

if ur== '':
    print "She weigh ", 2000 - 100 + 50,"pounds"
else:
    print 'SOrry!'
Shivkumar kondi
  • 6,458
  • 9
  • 31
  • 58
0

As others have said, it's because input in python 3 behaves like raw_input did in python 2. So one solution would be to use raw_input to get this to work in python 2.

However if you're following this book which seems like it's using python 3 I think you're much better off switching to python 3 now and you shouldn't run into these issues further along in the book.

GP89
  • 6,600
  • 4
  • 36
  • 64