0

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!

2
  • well, the previous line seems to missing a ) line = input("integer: " Commented Dec 25, 2011 at 21:25
  • 1
    You've changed your question in response to our answers, which makes this question less useful. A good way to fix this would be to post the original, a horizontal line, and then your edits that happened in response to our answers. Commented Dec 25, 2011 at 21:50

5 Answers 5

6

line 9 seems to missing a )

change:

line = input("integer: "

into

line = input("integer: ")

The except line need to be indented to match the try

and the lines:

total += number
count += 1

needs to be indented as well otherwise, the if and the else statements don't line up. I.e. code should be something like this:

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)
Sign up to request clarification or add additional context in comments.

Comments

1

You forgot to close your input() call on the previous line.

Comments

1

You need a closing bracket:

line = input("integer: ")

4 Comments

@BillyBach, the indentation for except does not match the one for try
Your updated code has the "total += number" line and the "count += 1" lines un-indented relative to the if block. You should indent those. Also, please re-edit your question to include the original and your edit to the code so that our answers make sense.
updated the original code instead of posting repeat blocks of code with corrections
@Sancho alright i see what your saying, ill post the corrected blocks
0

In addition to the closing parenthesis issue, your line that says except ValueError as err: is not indented enough (its indentation level should match that of the try statement). That should fix the line 18 else error you mentioned above.

2 Comments

by "it should match that of the try statement" do you mean: try: and then the statement on the same level of the try: or one in under the try: statement?
the try and except lines should have the same number of spaces in front of them. Fredrik answered it better than I, you should look at his answer
0

The lines starting with total += and count += need to start with eight spaces instead of four.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.