此代碼是來自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使用IDLE作爲一個IDE使用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
固定碼:
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)
謝謝!
好吧,上一行似乎缺少一個')'line = input(「integer:」' – 2011-12-25 21:25:53
你已經改變了你的問題以迴應我們的答案,這使得這個問題變得不那麼有用。這將是發佈原始的,水平線,然後發佈您的編輯以迴應我們的回答。 – 2011-12-25 21:50:20