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.
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.
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.
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
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
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.
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)
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…