Print all three students average points without creating three “print”-strings?


#1

https://www.codecademy.com/courses/learn-python/lessons/student-becomes-the-teacher/exercises/its-okay-to-be-average-?action=resume_content_item

How do I print all three studens average points without creating three “print”-strings?

E.g.

print get_average(lloyd)
print get_average(alice)
print get_average(tyler)
lloyd = {
  "name": "Lloyd",
  "homework": [90.0, 97.0, 75.0, 92.0],
  "quizzes": [88.0, 40.0, 94.0],
  "tests": [75.0, 90.0]
}
alice = {
  "name": "Alice",
  "homework": [100.0, 92.0, 98.0, 100.0],
  "quizzes": [82.0, 83.0, 91.0],
  "tests": [89.0, 97.0]
}
tyler = {
  "name": "Tyler",
  "homework": [0.0, 87.0, 75.0, 22.0],
  "quizzes": [0.0, 75.0, 78.0],
  "tests": [100.0, 100.0]
}

# Add your function below!
def average(numbers):
  total = sum(numbers)
  total = float(total)
  total = total / len(numbers)
  return total 
  
def get_average(student):
  homework = average(student["homework"])
  quizzes = average(student["quizzes"])
  tests = average(student["tests"])
  return 0.10 * average(student["homework"]) + 0.30 * average(student["quizzes"]) + 0.60 * average(student["tests"])

#2

Have you considered looping over students list?


#3

Yeah I have. But I can’t figure out how to do that :slight_smile:

Can you give me a few hints?


#4
students = [lloyd, alice, tyler]

Note that the list contains object identifiers, not strings. It is a convenient way to access all three dictionaries in one place.

The dictionaries are not stored in the list, only their references.

Hint

for student in students:
    # code here

#5

I got it :slight_smile:

students = [alice, tyler, lloyd]
for student in students:
  print  + str(get_average(student))
  print

Another question - I would like to have their name before the avarage point for each student.

I was thinking if I could create a new dictionaries but also thought that it should be possible to get the names by using the existing dictionaries?

Can you give me a hint again :slight_smile:


#6
for student in students:
    print ("%s: %.2f" % (student['name'], get_average(student))

#7

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