1

例を無視してください、それは私が現在学んでいる本の中にあります。

7はPythonをサポートしていないため、これをNetbeans 6.9.1で実行しています:(出力コンソールで実行しようとするとエラーが発生します。コードは、教科書に書かれている内容とまったく同じです。私が考えることができるのは、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

-みんなありがとう。

4

1 に答える 1

5

問題は、input()Python3.xでは何か違うことを意味しているということです。Python 2.xでは、同等の関数はraw_input()です。

input()toの呼び出しをtoの呼び出しに置き換えるだけ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")

これが問題を引き起こした理由は、Python 2.xでinput()ユーザーテキストを取得し、それをPython式として解釈するためです。無効な式である空白行を指定しているため、例外がスローされます。

Python 3.xを学習している場合は、別のエディターを使用することを強くお勧めします。PyCharmは素晴らしいです(無料ではありませんが)、そしてEclipse+Pydevはそこにあります。正直なところ、Python用のIDEは本当に必要ありません。コードの強調表示をサポートするGeditのような優れたテキストエディタだけが本当に必要です。

また、Pythonでは完全に冗長なセミコロンを削除したことにも注意してください。

于 2012-05-01T15:42:26.733 に答える