Question about lists and dictionaries in 5/9


#1

<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>

<Below this line, add a link to the EXACT exercise that you are stuck at.>
https://www.codecademy.com/courses/learn-python/lessons/student-becomes-the-teacher/exercises/its-okay-to-be-average-?action=lesson_resume&link_content_target=interstitial_undefined

<In what way does your code behave incorrectly? Include ALL error messages.>
It behaves fine, just have a question.

<What do you expect to happen instead?>
When taking inputs for the average(numbers) function, why does it ignore “name” and not throw an error for a string?



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

#2

the only thing the average function is designed to do is calculating the average value of a list with numbers (float and/or integers)

functions only execute when called, you haven’t called the function yet. So its only getting interpreted by the python interpreted, it doesn’t actually execute

as such, the function parameter (numbers) is only a placeholder until it gets its value from argument at function call

lets clarify all this by adding a function call:

def average(numbers):
  total = sum(numbers)
  total = float(total)
  total = total / len(numbers)
  return total

average(lloyd['homework'])

here i calculated the average grade for lloyd his homework

so the function parameter (numbers) gets its value from the function argument (lloyd['homework']) at function call

if we where to pass the name into the function:

average(lloyd['name'])

we would get an error


#3

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