Lesson 7/9


#1

So here’s my code :

 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)
  return total/len(numbers)

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

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

And here is what the lesson is saying the right code is:

# Add your function below!
def average(numbers):
  total = sum(numbers)
  total = float(total)
  return total/len(numbers)

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

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

Could the spaces at on the elif statements at the last part of the code make that much difference ??? and if so then why is the first on have a space at if score >= 90: ???


#2

I just copied and pasted from the lesson, sorry if those letters are bold; didnt meant to do that.


#3

The biggest difference between the two is the correct answer prints the final line in global scope whereas your code prints it inside the code block (which is never seen since it follows a return.


#4

Can you explain how this happens ? I am sorry if I seem rude, I just would like to understand this better to prevent for making a mistake like this in the future. Thank you !!!


#5

It happened because you indented the last line when it should not have been. When calling a function, be sure you are not inside it or another function, and especially do not write statements after return since they can never be reached.


#6

thank you !!! I will look out for that in the future !!!


#7

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