Student Becomes the Teacher (Codecademy Python tutorial)(8/9)


#1
lloyd = {
  "name": "Lloyd",
  "homework": [],
  "quizzes": [],
  "tests": []
}
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]
}

lloyd["homework"] = [90.0,97.0,75.0,92.0]
lloyd["quizzes"]=[88.0,40.0,94.0]
lloyd["tests"]=[75.0,90.0]

students  = [lloyd,alice,tyler]
print students


for student in students:
  print student["name"]
  print student["homework"]
  print student["quizzes"]
  print student["tests"]
  print
  
def average(numbers):
  total = float(sum(numbers))
  total /= len(numbers)
  return total

def get_average(student):
  homework = average(student["homework"])
  quizzes = average(student["quizzes"])
  tests = average(student["tests"])
  a = 0.1 * homework + 0.3*quizzes + 0.6*tests
  return a

def get_letter_grade(score):
  if score >= 90:
    return "A"
  elif score < 90 and score >=80:
    return "B"
  elif score < 80 and score >=70:
    return "C"
  elif score < 70 and score >=60:
    return "D"
  else:
    return "F"
  
b =  get_average(lloyd)
print get_letter_grade(lloyd)

problem - if i call get_letter_grade(lloyd), i get A

however if i first get lloyds average and set it equal to the avergae b, in that case the grade chnages to B.
why does this happen?
(Grade B is correct)


#2

get_letter_grade only purpose in life is to convert number grade to letter grade, but if we look at your function call:

print get_letter_grade(lloyd)

you pass a dictionary (lloyd) as argument, your get_letter_grade function is not build to handle a dictionary


#3

So what should i do then?


#4

That is the question, i on purpose left that open for you to figure out. What do you think you should do?


#5

leave python.
just kidding.
imma think.


#6

the thing is if i first get the average and then use the average to get the grade, the code seems lathargic, unoraganised and un-necessarily unoptimised.


#7

Doesn’t have to be the case, sometimes you might only want the number grade (calculated by get_average) while other times you want the letter grade. Which is why we need separate functions

this comes together nicely in the next exercise where you build the get_class_average function, you will see.


#8

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