8.Part of the wole


#1

students = [lloyd, alice, tyler]
def get_class_average(students):
results = []
for student in students:
results.append(get_average \
(student)
return average(results)

return average(results)
^
SyntaxError: invalid syntax

Someone can help me? What's wrong?


#2

look at your parentheses on the line above


#3

Sorry, but i don't understand.


#4

results.append(get_average \
(student)

look at the parentheses, do the number of opening parentheses match the number of closing parentheses?


#5

I love you ahaahah now it works!


#6

Hi there. I can't figure out what's wrong, it seems pretty much the same as yours (pasted below). I'm getting this error: Oops, try again. get_class_average([alice, lloyd]) returned 91.15 instead of 85.85 as expected.

Any suggestions? Much appreciated!

lloyd = {
    "name": "Lloyd",
    "homework": [90.0, 97.0, 75.0, 92.0],
    "quizzes": [88.0, 40.0, 94.0],
    "tests": [75.0, 90.0]
}
alice = {
    "name": "Alice",
    "homework": [100.0, 92.0, 98.0, 100.0],
    "quizzes": [82.0, 83.0, 91.0],
    "tests": [89.0, 97.0]
}
tyler = {
    "name": "Tyler",
    "homework": [0.0, 87.0, 75.0, 22.0],
    "quizzes": [0.0, 75.0, 78.0],
    "tests": [100.0, 100.0]
}

# Add your function below!
def average(numbers): 
    total = sum(numbers)
    total = float(total)
    total = total/len(numbers)
    return total
    
def get_average(student):
    homework = average(student["homework"])
    quizzes = average(student["quizzes"])
    tests = average(student["tests"])
    return (.1 * homework) + (.3 * quizzes) + (.6 * tests)

def get_letter_grade(score):
    if score >= 90:
        return "A"
    elif score >= 80 and score < 90:
        return "B"
    elif score >= 70 and score < 80:
        return "C"
    elif score >= 60 and score < 70:
        return "D"
    else:
        return "F"
students = [lloyd, alice, tyler]
def get_class_average(students):
    results = []
    for student in students:
        results.append(get_average(student))
        return average(results)

#8

a function ends the moment a return keyword is reached

if a return keyword is reached in the loop, the function needs to end, so the loop will break

so, in its first iteration, the loop breaks, is this what you want?


#9

I don't get this concept either. It isn't referenced enough anywhere. You have to take the next lesson on faith. Doesn't encourage persistence. If this is so, please include a discussion of continuence rather than making students feel dumb.


#10

I can't figure out what I'm doing wrong:


#11

^ did you read this?

second, the students list should be outside the function. You can supply students list as argument when calling the function

by placing students list inside the function, you overwrite the function parameter. This way, your function only works for 3 students. It should work for a class of any size (one, two or three students)


#12

Hey I ran into the same problem as you, and fixed it by solving the indentation of the 'return' line. Basically it should be the same indentation as the 'for' line, I believe stetim94 was explaining that the positioning of the return keyword meant that the function ends prematurely.


#13

yea, exactly. If you class consist of 3 students, the loop should make 3 iterations. But given your return keyword is inside the loop, in the first iteration it breaks, causing the loop to end prematurely.


#14

Hi bro, this is your prob check this pic.

But, I think some have already answered you @stetim94, even so, here is my contribution. I also went wrong with the indentation. :sweat_smile:


#15

Guys it's literally the indentation messing you up, just backspace your last return statement, and you will be all set


#17

Thanks guys, it's all fixed thanks to your help!


#18

You're welcome! :slight_smile:


#19

I made this same mistake. If you asked me, this is an easy mistake to make and I wouldn't feel too bad about it. In a language like javascript, this would be easier to see.

You just want the return statement after the loop has executed. If you have it inside, it will execute, and the loop will terminate.


#20

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