FAQ: Learn Python: Student Becomes the Teacher - Just Weight and See

This community-built FAQ covers the " Just Weight and See" exercise in Codecademy’s lessons on Python.

FAQs for the Codecademy Python exercise _ Just Weight and See_:

Join the Discussion. We Want to Hear From You!

Have a new question or can answer someone else’s? Reply (reply) to an existing thread!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources about Python in general? Go here!

Want to take the conversation in a totally different direction? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account, billing, Pro, or Pro Intensive? Reach out to our support team!

None of the above? Find out where to ask other questions here!

Other FAQs

The following are links to additional questions that our community has asked about this exercise:

  • This list will contain other frequently asked questions that aren’t quite as popular as the ones above.
  • Currently there have not been enough questions asked and answered about this exercise to populate this FAQ section.
  • This FAQ is built and maintained by you, the Codecademy community – help yourself and other learners like you by contributing!

Not seeing your question? It may still have been asked before – try (search) in the top-right of this page. Still can’t find it? Ask it below by hitting the reply button below this post (reply).

A post was split to a new topic: Says I have an indent

2 posts were split to a new topic: Error: List Indices Must Be Integers, Not str

2 posts were split to a new topic: What is wrong with this code?

Hi,

I have completed this exercise but I want to know how the computer recognises that the ‘average()’ is the average of the contents in the brackets, since it still shows up as red letters?

Cheers
Josh

The values in square brackets are lists. We pass a list to the average function, where it is tallied up to arrive at a total, then divided by the number of items in the list (len(numbers)) to return an average.

The values in the students list are names of references that point to individual student dictionaries. When we pass one of them into the get_average() function, the function is able to see inside that dictionary and extract the data.

1 Like

Okay thanks.

So these functions have nothing to do with the student dictionaries from line 1-17? I can’t see anything in lines 21-30 that references them.

Thanks

Those lines contain only the data objects. The functions are the means of extracting and computing the data, both individually, and as a whole (class_average).

Okay, but at which point in the functions does it use the data objects? I’m just trying to pinpoint the code that does this as I am struggling to understand how Python knows to use the student data without actually mentioning the variable names.

Is it between line 27 and 29 where the argument of the function followed by the key in square brackets is?

Why would it need to care about or know that?
Maybe it should operate on whatever data is provided to it when it is called.

If you have a function named add, then it should probably add the two numbers provided to it instead of always returning 12 because it’s decided 5 and 7 are the numbers that need adding. Being able to tell add which values to operate on is what makes add useful.

1 Like

We provide a list of references (the variables to which you refer?) and it is our program, not Python, that extracts the data according to our needs. WE are the ones who know about the data, and how it is organized, so WE must write the code to give us the outcome we desire.

def get_class_average(class_list):

called on the students list,

print (get_class_average(students))

Given that we are passing in a list, then we must iterate over that list to gain access to the individual student’s data., by name.

for student in class_list:

Note that the name class_list is arbitrary, but it does tell the reader what the inbound object should represent.

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):
  homerwork= average(student["homework"])
  quizzes= average(student["quizzes"])
  tests= average(student["tests"])
  return (homework*0.1)+(quizzes*0.3)+(tests*0.6)

i get this error with y code get_average(alice) raised the following error: global name ‘homework’ is not defined can someone explain to me why ? are the values or ’ and " different?

Might give the spelling here a quick check. :wink:

Not sure if there might be something else amiss. When you paste code in a post, make sure you use the </> button in the menu bar. I find it easiest to click the </> first, and then paste code in the space indicated. It will preserve indentation, formatting and special characters, so the rest of us can see your code the way you originally typed it.

How does it know that g in def get_average represents that dictionary and lists?

  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(n):

  total = sum(n)

  total = float(total)

  return total / len(n)

def get_average(g):

  homework= average(g['homework'])

  quizzes= average(g['quizzes'])

  tests = average(g['tests'])

  return (homework*0.1)+(quizzes*0.3)+(tests*0.6)

g in the above is an object, one of the dictionaries, alice, tyler or lloyd. The function would be called with one of those references as the argument.

get_average(alice)  
# g == {
    "name": "Alice",
    "homework": [100.0, 92.0, 98.0, 100.0],
    "quizzes": [82.0, 83.0, 91.0],
    "tests": [89.0, 97.0]
  }

Thanks for the update, but I am still a little confused on how exactly it knows to pull that dictionary.

do you know how to modify this as a print statement to better understand or produce the results of

homework=average(g['homework]) ?? I’ve been trying to figure it out but I cant lol

i figured it out lol…

print get_average(alice)

Thanks this ismaking a little more sense

1 Like

g is the formal parameter of the function. It is a placeholder, in a sense, the variable by which the argument is referred inside the function.

 arg      param
alice  =>   g

so basically until i call that function with an argument that is a dictionary or list with those parameters it won’t do anything.

and it will fail if it does not have those parameters?

1 Like