8.Part of the Whole - Syntax error on a return



When i try to run the code I receive the following message:
File "python", line 45
return average(results)
SyntaxError: invalid syntax

it seems to be a Syntax error on the return line of the get_class_average function but i can't figure out what exactly i did wrong. i copied the function code below, line 45 is the return line

def get_class_average(students):
    results =[]
    for student in students:
    return average(results)


look at the line above the error message, pay special attention to your parentheses


wow i can't believe i didn't notice i missed the second barcket

still, weird how the editor was pointing to the line below, is it because it didn't know where to end the line before the return?


its surprising difficult to spot.

well, the thing with parentheses is that we can continue coding on the next line:


is perfectly valid. So python assumes you wanted to continue coding on the next line, but then encountered syntax which didn't fit with what you where doing in the parentheses.

Don't stare yourself blind on a single line (tunnel vision), always focus on the bigger picture.


thank you im used to ending sentences with semicolons like in C, so I have a hard time noticing when python decides the statement breaks


but forgetting a semi-colon in C can have disastrous consequences as well, good luck finding it in a 1000+ lines of code :stuck_out_tongue:


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.