What is wrong with this code?


could you plz tell me what is wrong with this code?

def get_average(student):
for s in student:
total = 0.1 * average(s[“homework”]) +
0.3 * average(s[“quizzes”]) +
0.6 * average(s[“tests”])
print total

print get_average(tyler)

FAQ: Learn Python: Student Becomes the Teacher - Just Weight and See

get_average job is to calculate the (weighted) average grade of a single student and return this average.

So not sure why you use a loop


Thank you

By using a loop I was trying to give the Function more than one student and the loop do the average function for all of them so, for 2 students the outcome would be 2 weighted average.
Is it possible?


possible? Yea. Wise? No

in 1 or 2 exercises time you will build a get_class_average function which can calculate the average grade for a class of any size, in the get_class_average function you use get_average as helper function. So its not wise to change how get_average works.


I totally got it. It can be coded more easily.
but whenever I run this code an error occurs about string.
could you please tell me where did I code wrong in the code above. I just want to learn my mistakes

thank you


I already made suggestions? Like removing the loop? This means you made changes, which means the error might not be in the code you posted, so if you make changes, and run into an error, and need help, post an updated version of your code. That can save a lot of confusion.


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