8/9 question pls help


#1

My code is

def get_class_average(class_list):
  class_list=[lloyd,alice,tyler]
  results=[]
  for student in class_list:
    student_avg=get_average(student)
    results.append(student_avg)
  return average(results)

but it gets wrong
and the correct one is:

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

The difference is the correct one does not include class_list=[lloyd,alice,tyler]

So my question is why we don’t need to define this? I assume that Python doesn’t know what’s class_list, so I need to tell him!


#2

look:

def get_class_average(class_list):
  class_list=[lloyd,alice,tyler]
  results=[]
  for student in class_list:
    student_avg=get_average(student)
    results.append(student_avg)
  return average(results)

print get_class_average([lloyd, alice])

i ask your function to calculate the class average for 2 persons, but your overwrite the parameter, so it still calculating the class average for 3 persons. Which is not what i want

parameters can be undefined, given functions are only executed when called, that is where the parameter get there value from (from argument at function call). So class_list is a placeholder, until the function is called

this is the great thing about function, now we can calculate the class average for a class of any size! :slight_smile:


#3

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