Question

Please ignore the example, it is just in a book I am currently learning from.

I am running this in Netbeans 6.9.1 as 7 doesn't support Python :( and I am getting a error when trying to run it in the output console. The code is exact as to what is written in the text book. The only thing I can think of is that net beans only supports 2.7.1 yet the book I am learning from is Python 3.1. Could this be the issue? Please let me know if I have overlooked something.

Here is the basic script;

# Word Problems
# Demonstrates numbers and math

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); 

input("\n\nPress the enter key to exit");


Traceback (most recent call last):
  File "/Users/Steve/Desktop/NewPythonProject/src/newpythonproject.py", line 6, in <module>
    input("Press the enter key to find out.");
  File "<string>", line 0

^
SyntaxError: unexpected EOF while parsing

-Thanks guys.

Was it helpful?

Solution

The issue is that input() means something different in Python 3.x. In Python 2.x, the equivalent function is raw_input().

Simply replace your calls to input() with ones to raw_input() and it will work as expected:

# Word Problems
# Demonstrates numbers and math

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)

raw_input("\n\nPress the enter key to exit")

The reason this caused a problem is that in Python 2.x, input() takes user text, then interprets it as a Python expression. As you were giving it a blank line, which is an invalid expression, it throws an exception.

I would highly suggest using a different editor if you are learning Python 3.x. PyCharm is great (albeit not free), and Eclipse+Pydev is out there. To be honest, you don't really need an IDE for Python - a good text editor like Gedit that supports code highlighting is all you really need.

Also note I removed the semicolons, which are entirely redundant in Python.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top