-1

Trying to print out some numbers from a 2 dimensional list.

I have one function that needs to print out the average grade per student. The other function should print out the average of all the students together.

How can I acces these numbers and make them function the way I want to? I tried appending them to a list so that I can get the 4 averages and then go /4 but it's only appending the last average number.

studentgrades = [ [95, 92, 86],[66, 75, 54],[89, 72, 100],[34, 0, 0] ]
def average_per_student(studentgrades):
    child = 0
    lst_average = []
    for cijfers in studentgrades:
        average = int(sum(cijfers) /3)
        child += 1
        result = 'child %d: gemiddelde %d' % (child, average)
        lst_average.append(result)
        vg = []
        print(average)
        vg.append(average)

    print(vg)
    return lst_average


def average_of_all_students(studentgrades):
    pass

resultaat1 = average_per_student(studentgrades)
# print(resultaat1)
resultaat2 = average_of_all_students(studentgrades)
# print(resultaat2)
Some Name
  • 561
  • 6
  • 18

1 Answers1

0

You need to move vg = [] outside the for loop, so that you aren't re-initializing it each time. Then you will have initialized it once, appended one element per student, and in the end it will have the full list of 4 student averages.

Curtis Lusmore
  • 1,822
  • 15
  • 16
  • Thanks, I feel dumb for not seeing that. How would I go about returning multiple values in 1 print statement? I want to remove the comment on print(resultaat1) and print all the averages from the students with a return in my function. – Some Name Sep 14 '16 at 08:48
  • I'm not quite sure I understand your question. You can return both the `lst_average` and the `vg` values by doing `return (lst_average, vg)` as a tuple. You would then need to update calling points to be `(average_strings, average_values) = average_per_student(studentgrades)` or something like that. – Curtis Lusmore Sep 14 '16 at 09:01
  • What I'm trying to achieve, is instead of putting a print() in my function, I want to use a return. However, when I use the return, I don't get the same values I get as I get with print(). – Some Name Sep 14 '16 at 09:17
  • Oh okay, I understand. There is a huge difference between `return` and `print`. Perhaps see this: http://stackoverflow.com/questions/7664779/python-what-is-the-formal-difference-between-print-and-return – Curtis Lusmore Sep 14 '16 at 09:30