How to fix invalid syntax error at 'except ValueError'?

python, python-3.x

Solution

There are two things wrong here. First, You need parenthesis to enclose the errors:

except (ValueError,IOError) as err:

Second, you need a `try` to go with that `except` line:

def average():
    try:
        TOTAL_VALUE = 0
        FILE = open("Numbers.txt", 'r')

        for line in FILE:
            AMOUNT = float(line)
            TOTAL_VALUE += AMOUNT
            NUMBERS_AVERAGE = TOTAL_VALUE / AMOUNT
        print("the average of the numbers in 'Numbers.txt' is :",
            format(NUMBERS_AVERAGE, '.2f')) 

        FILE.close()

    except (ValueError,IOError) as err:
        print(err)

`except` cannot be used without `try`.

Problem

I'm trying to write a simple exception handling. However it seems I'm doing something wrong. ``` def average(): TOTAL_VALUE = 0 FILE = open("Numbers.txt", 'r') for line in FILE: AMOUNT = float(line) TOTAL_VALUE += AMOUNT NUMBERS_AVERAGE = TOTAL_VALUE / AMOUNT print("the average of the numbers in 'Numbers.txt' is :", format(NUMBERS_AVERAGE, '.2f')) FILE.close() except ValueError,IOError as err: print(err) average() > line 14 > except ValueError as err: > ^ > SyntaxError: invalid syntax ```

Original source