Whats wrong with my code?


#1

grades = [100, 100, 90, 40, 80, 100, 85, 70, 90, 65, 90, 85, 50.5]

def grades_sum(grades):
    total = 0
    for i in grades:
        total = total + i
    return total
print grades_sum(grades)
def grades_average(grades):
    average = grades_sum(grades)/(float(len(grades))
    return average
print grades_average(grades)

This is returning a syntax error for line 11, on return average. Thanks for reading my question, and thanks for your answer


#2

)


#3

Wow, such a thoughtful response, thanks for ur answer, i really appreciate it. I'm not sure, but i don't agree completely on the technicality of ur response. OH, also u have some grammar and writing issues. The issue is that... well, actually, i don't see any issue, ur comment is flawless, u solved all my problems (hint hint, i'm not sure at ur inteligence now, based off of that response, but i hope that u realize i am being sarcastic). NOW, CAN SOMEONE PLEASE ACTUALLY ANSWER MY QUESTION???


#4

You dropped it. :wink:


#5

OH, dam, u might have been a little clearer in ur reply? agreed? but yeah, that fixed the problem. next time, can u give a little more effort into ur reply? thnx


#6

Hey I worked hard to circumvent the 20 character requirement there! Haha sorry about the frustration.


#7

hey its fine, as long as i have an answer, and can move on from my careless errors,to continue learning. and what is this 20 character requirement u speak of?


#8

discuss doesn't accept posts that are too short.


#9

So what happened there was that return didn't make sense while a parenthesis was open, that's why it pointed at the line below where the mistake really was - it tells you where it stops being valid syntax, not where the mistake is.


#10

yeah, i know, i got it, but why did it return an error on line 10, for an unpaired parentheses, but rather, it threw an error on the return statement, it doesn't make sense, or at least, i don't understand how to read the errors in the window yet


#11

Python's error's are really nice, read them, figure out how they relate to the mistake so that you understand them better next time you get them.

That's what I just tried to explain :confused:


#12

what does it mean when u say where it stops being valid syntax? isn't the unpaired parentheses itself already "stops being valid syntax" because its not correct, please explain in greater detail, as i am obviously unsure about that


#13

This is valid:

print (



'hello')

Wanting to split up function calls, list/dict/tuple literals etc is common, so newlines are ignored when there are open brackets.

(note that print is not a function in Python2.. it is in Python3 though. This is why I've put a space after print, because I'm not calling it, I'm just adding parentheses around my string.)


#14

ah, i see, i think i understand now, so it assumed that there was the possibility that the parentheses could be closed in a future line, but then it encountered us calling on that very line that has an unpaired variable, and then the syntax stopped being viable, correct?


#15

meant unpaired parentheses


#16

It was invalid because this is not valid:

(return)

Same thing as that you wouldn't put a loop or a function inside parentheses.

(for i in range(5): print 'hello')

This on the other hand is valid:

(n for n in range(5))

But that is an expression, not a statement - it's a generator expression.


#17

oh, so it thought i put the return INSIDE the parentheses, thinking the parentheses would be closed in the future, so it was as if the return was invalid, because it was inside the parentheses. correct?


#18

Yeah, no, that's gibberish :frowning:

Surely you thought that you had closed the parentheses?

An expression was expected, but you had a statement instead, like trying to put in a puzzle piece of the wrong shape, it can't be there regardless of what's painted on that piece.


#19

yes, i thought i closed the parentheses, but i'm asking u from the point of the computer, the window, the program, etc. The program doesn't know my aims or goals. So I want to understand how it goes and checks through the program. So, from the perspective of the computer, tell me if this line of reasoning is correct, from the perspective of the computer:
line 10: oh, i see a variable defined, oh whats here?? whats this?? an unpaired parentheses. oh wait, he will close them on a future line, ok, the line ends, lets go to line 11
line 11: uh oh, we have a return statement before we closed the parentheses, meaning this return is supposedly inside the hypothetical future closing parentheses. but that's invalid syntax, u can't have a return statement inside parentheses. therefore, let us stop this program, and say invalid syntax on line 11.

This is what I was trying to say. Now is this reasoning correct?


#20

The parser is always expecting one of several patterns, based on what it has read so far, for example if it's at the top level and reads def, then a name has to follow, and then ( and then comma-separated args, and then ) and then : and as soon as something is read that doesn't match what's currently allowed, then a syntax error is raised.

A statement while there's an open ( doesn't match any valid pattern, so a syntax error is raised.

Here's the syntax specification for Python3:
https://docs.python.org/3/reference/grammar.html

And the syntax for a function is:

funcdef: 'def' NAME parameters ['->' test] ':' suite

Which should be recognizable, NAME, parameters, test, suite are defined in a similar fashion and re-used. the stuff inside [ ] is optional.

See that suite is required? That's why this is invalid:

def hello():

If no suite follows.

And a suite is defined as:

suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT

So a simple_stmt can follow immediately, or there can be a newline and the indentation level can be increased and then one or more (+) statements (stmt) and then the indentation level needs to return back to what it was.

.. A Python file is defined as:

file_input: (NEWLINE | stmt)* ENDMARKER
Which is any number of newlines or statements followed by end of file. Makes sense, right.