Computing the Average


#1

anyone? I'm stuck
grades = [100, 100, 90, 40, 80, 100, 85, 70, 90, 65, 90, 85, 50.5]

def grades_sum(scores):
total = 0
for i in scores:
total += i
return total
grades_sum(scores)

print grades_sum(grades)

def grades_average(grades):
total = 0
for i in grades:
new_avg = float(total) / len(grades)
return new_avg
grades_average(grades)

print grades_average(grades)

and I get - Oops, try again. You did not print the correct average. Double-check your code and make sure you print the average!


#2

Hi @shiransiboni,

A few comments for you regarding your code:

1.. Double calling functions

Throughout your code, you have a tendency to call your functions twice in succession, like this:

and this:

which shouldn't be the case - just call your function once, when you need it!

2.. what you did in your grades_average function

Let's take a look at your function again:

When we consider your for loop, we see that we are taking new_avg to be float(total) / len(grades). However, did you notice that you just defined total as 0 right before your for loop? This means that your new_avg will forever be equal to 0, regardless of whatever is eventually passed into the function!

To remedy this, consider following the instructions more closely, and calling the grades_sum function within your grades_average function.

3.. Your choice of parameter for the grades_average function

As you can see here, you decided to just use grades directly as the parameter in your grades_average function. By doing this, you a) potentially confuse yourself when passing in the actual grades list as an argument and b) are defying the stated instructions again.

Although technically speaking, I think it is fine if you use grades as your parameter; the code would still work (so long as it is consistently used within the function). I just don't think it is really advisable though :stuck_out_tongue: Also, the instructions are there for a reason, unless you're an experienced programmer looking for some fun :stuck_out_tongue:

4.. Your question is placed under the wrong unit.

Instead of 11. Student Becomes the Teacher, it should be 16. Exam Statistics. It would be good if you could be more careful next time, both for the answerer and other curious coders reading this forum :slight_smile:

Hope this helps!