This code is example code from a book on python. It is a simple program to enter integers and display the sum, total count, and average of the integers. However, when I try to run the code, I receive a syntax error at line 18, the colon. This code looks perfectly fine to me. Any ideas?
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)
When i try and run this, I get an error:
File "./intproj.py", line 18
else:
^
SyntaxError: invalid syntax
I am using IDLE as an IDE with python 3.2.2 on Ubuntu 11.10
updated code:
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)
and now get the error:
File "./intproj.py", line 18
else:
^
SyntaxError: invalid syntax
Fixed code:
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)
Thanks!
)line = input("integer: "