# Part the whole ... advice if needed

def get_class_average(students):
results =
for student in students:
get_average(student)
results.append(get_average(student))
return average(results)

2 Likes

My code is exactly the same as the one posted here, but it does not work.

my code:

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]
}

def average(numbers):
total = sum(numbers)
total = float(total)
mean = total / len(numbers)
return mean

def get_average(student):
homework = average(student[âhomeworkâ])
quizzes = average(student[âquizzesâ])
tests = average(student[âtestsâ])
return 0.1 * homework + 0.3 * quizzes + 0.6 * tests

if score >= 90:
return âAâ
elif score >= 80:
return âBâ
elif score >= 70:
return âCâ
elif score >= 60:
return âDâ
else:
return âFâ

def get_class_average(students):
results =
for student in students:
get_average(student)
results.append(get_average(student))
return average(results)

Also, can you enclose your code in backticks so it is properly formatted?

^ These are backticks.
Put three backticks on the line before your code.
Put three more backticks on the line after your code.

@papayabubble: What answer runs properly when I try it. Are you receiving an error message? If so, what does it say?

In terms of advice, Iâd just ask: Why include this line? What does it do for you? Do you lose anything if you remove it from your code?

You need to use:
get_average(student)
because for a class average you need to add the averages from the different students. If you donât put it in your class average wonât have any numbers to work on. Get it?

Youâre right the you need to do that at some point, but you do it twice.

[quote=âpapayabubble, post:1, topic:25687â]`get_average(student) results.append(get_average(student))`[/quote]
Do you need the first instance?

1 Like

You donât need it the first time but I put it in there to understand which step Iâm at compared to the instructions.

Did you make sure your indentation was correct?
Or you can otherwise you could try it like this:

def get_class_average(students):
results =
for student in students:
results.append(get_average(student))
return average(results)

I tried and it does not work!

The reply of the system is: ââOops, try again. get_class_average([alice, lloyd]) returned 91.15 instead of 85.85 as expectedââ

Write only the last method

def get_class_average(students):
âŚresults =
âŚfor item in students:
âŚget_average(item)
âŚresults.append(get_average(item))
âŚreturn average(results)

3 Likes

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]
}

def average(numbers):
total= sum(numbers)
total=float(total)
def get_average(student):
homework=average(student[âhomeworkâ])
quizzes=average(student[âquizzesâ])
tests=average(student[âtestsâ])
return 0.1homework+0.3quizzes+0.6 *tests
if score>=90:
return âAâ
elif score>=80:
return âBâ
elif score>=70:
return âCâ
elif score>=60:
return âDâ
else :
return âFâ
def get_class_average(students):
results=
x=get_average(students[0])
y=get_average(students[1])
z=get_average(students[2])
results.append(x)
results.append(y)
results.append(z)
q=average(results)
return (x,y,z,q)
plz help me where i am goining wrong
error is Oops, try again. get_class_average([alice]) resulted in an error: list index out of range

is there any suggestions i am stuck in this

I got the same error message. Then I adjusted the indentation on the last line of code and it compiled properly.

1 Like

iâve fix it try this code :

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]
}

def average(numbers):
total = float(sum(numbers))
result = total / float(len(numbers))
return result

#def get_average(lloyd,alice,tyler):
def get_average(student):
lloyd_homework=average(student[âhomeworkâ])
lloyd_quizzes=average(student[âquizzesâ])
lloyd_tests=average(student[âtestsâ])
lloyd_average_score=0.1lloyd_homework+0.3lloyd_quizzes
+0.6*lloyd_tests
return lloyd_average_score

n = lloyd
print get_average(n)

if(score >= 90.0):
return âAâ
elif score >= 80.0:
return âBâ
elif score >= 70.0:
return âCâ
elif score >= 60.0:
return âDâ
else:
return âFâ

students = [lloyd,alice,tyler]
def get_class_average(students):
results =
for item in students:
results.append(get_average(item))

``````return average(results)
``````

1 Like

Thank you for your kind cooperation. The key is to actually fix the indentation in the last line of code, stepping back âreturnâ.

1 Like

papayabubble, you are teaching yourself to write redundant code. This is messy and may cause issues on future assignments. You should comment out any redundant code that you write if you are going to leave it in to follow the instructions.