初学者Python书籍教程问题

1 python netbeans python-2.7 netbeans-6.9

请忽略这个例子,它只是在我正在学习的一本书中.

我在Netbeans 6.9.1中运行它,因为7不支持Python :(我在输出控制台中尝试运行时遇到错误.代码与教科书中的内容完全相同.我能想到的是net beans只支持2.7.1但我正在学习的书是Python 3.1.这可能是问题吗?如果我忽略了某些东西,请告诉我.

这是基本脚本;

# 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
Run Code Online (Sandbox Code Playgroud)

-多谢你们.

Gar*_*tty 5

问题是这input()意味着Python 3.x中有所不同.在Python 2.x中,等效函数是raw_input().

只需将您的电话更换为input()1 raw_input(),它将按预期工作:

# 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")
Run Code Online (Sandbox Code Playgroud)

这导致问题的原因是在Python 2.x中,input()获取用户文本,然后将其解释为Python表达式.当你给它一个空白行,这是一个无效的表达式时,它会抛出一个异常.

如果您正在学习Python 3.x,我强烈建议使用不同的编辑器.PyCharm很棒(尽管不是免费的),Eclipse + Pydev就在那里.说实话,你真的不需要一个适用于Python的IDE - 像Gedit这样的优秀文本编辑器支持代码突出显示,这是你真正需要的.

另请注意,我删除了分号,这些分号在Python中完全是多余的.