2011-12-25 17 views
1

このコードは、Pythonに関する書籍のサンプルコードです。これは、整数を入力し、整数の合計、合計数、および平均を表示する簡単なプログラムです。しかし、コードを実行しようとすると、18行目のコロンで構文エラーが発生します。このコードは私にとっては完璧に見えます。何か案は?単純なPython 3.2.2プログラムの構文エラー

print("type integers, each followed by Enter; or just Enter to finish") 

total = 0 
count = 0 

while True: 
    line = input("integer: " 
    if line: 
     try: 
      number = int(line) 
     except ValueError as err: 
      print(err) 
      continue 
    total += number 
    count += 1 
    else: 
     break 
if count: 
    print("count=", count, "total =", total, "mean =", total/count) 

私は試してみて、これを実行すると、私はエラーを取得:

File "./intproj.py", line 18 
    else: 
    ^
SyntaxError: invalid syntax 

私はUbuntuの11.10


更新されたコード上のpython 3.2.2とIDEとしてIDLE使用しています:

print("type integers, each followed by Enter; or just Enter to finish") 

total = 0 
count = 0 

while True: 
    line = input("integer: ") 
    if line: 
     try: 
      number = int(line) 
     except ValueError as err: 
       print(err) 
       continue 
    total += number 
    count += 1 
    else: 
     break 
if count: 
    print("count=", count, "total =", total, "mean =", total/count) 

ここでエラーが発生します。

File "./intproj.py", line 18 
    else: 
    ^
SyntaxError: invalid syntax 

固定コード:

print("type integers, each followed by Enter; or just Enter to finish") 

total = 0 
count = 0 

while True: 
    line = input("integer: ") 
    if line: 
     try: 
      number = int(line) 
     except ValueError as err: 
       print(err) 
       continue 
     total += number 
     count += 1 
    else: 
     break 
if count: 
    print("count=", count, "total =", total, "mean =", total/count) 

ありがとう!

+0

だけでなく、前の行は、 ')' 'ライン=入力(「整数:」行方不明に思わ' –

+1

あなたはこの質問にはあまり有用で作るこれ、私たちの答えに応じて、あなたの質問を変更した修正する良い方法を。これはオリジナルのもの、水平な線、私たちの回答に応じて行われた編集を掲示することです。 –

答えて

1

前の行のinput()コールを閉じるのを忘れました。

1

あなたは閉じ括弧が必要になります。

line = input("integer: ") 

line = input("integer: " 

line = input("integer: ") 
+0

@Fredrik:http:// jaを参照してください。wikipedia.org/wiki/Bracket –

+0

固定、 "else"、18行目で同様のエラーが発生する –

+0

@BillyBach、 'except'のインデントが' try'のインデントと一致しない –

6

ライン9は)

変化が行方不明に思えますラインはtry

や線を一致させるためにインデントする必要があります

total += number 
count += 1 

も同様にインデントするためにそれ以外の場合は、ifelse文が並んでいない必要があります。私。閉じ括弧の問題、except ValueError as err:が十分で字下げされませんと言うあなたの行(そのインデントレベルはtry文のことと一致する必要があります)に加えて

print("type integers, each followed by Enter; or just Enter to finish") 

total = 0 
count = 0 

while True: 
    line = input("integer: ") 
    if line: 
     try: 
      number = int(line) 
     except ValueError as err: 
      print(err) 
      continue 
     total += number 
     count += 1 
    else: 
     break 
if count: 
    print("count=", count, "total =", total, "mean =", total/count) 
+0

ありがとう!それを固定した –

0

:コードは次のようなものでなければなりません。それはあなたが上記のエラー18 elseエラーを修正する必要があります。 total +=count +=で始まる

+0

"それはtry文のそれと一致している必要があります"とはどういう意味ですか?try:と同じレベルのtry:またはtry:文の中にある文ですか? –

+0

'try'と' except'行は、それらの前に同じ数のスペースを持つ必要があります。フレドリクは私よりもそれに答えました、あなたは彼の答えを見なければなりません – ianweller

0

ラインは8つのスペースの代わりに4を起動する必要があります。