Why is the average returned incorrect?

Question

Why is the average returned incorrect?

Answer

Be sure to write your return statement outside of the loop, otherwise your loop can only possibly run one time before return is executed. As soon as a function executes a return statement, it exits immediately and gives whatever value that is.
Your return statement should be on the same indentation level as the for loop so that it’s inside of the function, but outside of the for loop.

4 Likes

2 posts were split to a new topic: Student is not defined

My issue was declaring my var outside of my function:

wrong:

results[]
def get_class_average(class_list):

right:

def get_class_average(class_list):
  results = []
6 Likes

For some reason it looks like my for loop is not including tyler. From my review it appears that my indentation is correct.

The code runs outside of that, but the I receive the following message:

get_class_average([alice, lloyd]) returned 80.55 instead of 85.85 as expected

Thanks as always for the responses. They help more than you know!

1 Like
def get_class_average(class_list):
  results = []
  for student in class_list:
    results = get_average(student)
return results

Study the code above and see if an issue doesn’t surface.

2 Likes

I tried that but I still get an error message that says ‘get_class_average([alice, lloyd]) returned 80.55 instead of 85.85 as expected’. I tried so many things but it doesn’t seem to work they way I want it to.

1 Like

So may I take it then that you have not found the error?

In the above, results will only be that last student in the class_list. We need to append to that list so it grows with each student average computed.

4 Likes

Thanks, man. You’re the best!!

3 Likes

That code depends on other code which you’re not showing.

You might want to test your function yourself, and consider whether you agree with that it should return a number and then see if the result is indeed a number, or not.

3 Likes

Im still confused, it seems like my .append does nothing and I’ve tried it several different ways

def get_class_average(class_list):
  results = []
  for student in class_list:
    total = 0
    total = total + get_average(student)
    results.append(total)
  return total

Ive also done return instead of total like in the example above and ive tried several different things in the .append()

I am very confused as to what is going wrong

def get_class_average(class_list):
  results = []
  total = 0.0
  for student in class_list:
    total += get_average(student)
    results.append(total)
  return total
def get_class_average(class_list):
  results = []
  for student in class_list:
    results = get_average(student)
    results.append(get_average(student))
  return results

none of these work, I cant remember all the other ways Ive tried to figure this out and nothing works

I scrolled through the forums and someone is saying that this should work but it isnt, it’s coming back with get_class_average([alice]) should return a number

def get_class_average(class_list):
  results = []
  for student in class_list:
    results.append(get_average(student))
  return results
2 Likes

In order for that to return a number we need to take the average.

return sum(results) / len(class_list)
1 Like

ugh, thank you.
is this a bad omen if I’m struggling this much this early on with seemingly simple things?

2 Likes

Not an omen, just an indication that your brain is working. Confusion is a good sign, not a bad one.

12 Likes

thank you again, I think I needed that as well

2 Likes
def get_class_average(class_list):
  results=[]
  for student in class_list: # this is for looping(making action for every student)
    results.append(get_average(student)) # this helps to add get_average result into results list
  return average(results)    # returns result
4 Likes

Really, why does the code on the right is wrong and the code on the left is right? (The rest of the code is exactly as the exercise.
It says exactly the same thing with a different nomenclature.

My full code:

Those do very different things. When you read it, make sure you’re not reading what you expect it to say, because then you’ll skip over the difference just because you don’t expect it to be there.

You can also edit one to match the other, when they are more similar you may have an easier time finding the difference. You can also run it through a diff program once you think they’re identical (they’re short enough to eyeball for the difference though) (and you still have to pay attention to what edits you’re making, obviously, or you may end up eliminating the difference without noticing)

@ryan.hurley.btc
When you are adding the weights to the quizzes tests and homework it should be multiplied (*), not added.

like this:

 total = homework *.1 + quizzes * .3 + tests * .6

Is there a way to print this result so we can see what the average is? Just spent half hour trying to print the result LOL

We can insert print statements at any point in our program, then comment them out after we’ve seen what prints at that point. To see the final result, we can print the call expression…

print (get_class_average(students))