Part of a Whole


#1



https://www.codecademy.com/en/courses/python-beginner-en-qzsCL/1/4?curriculum_id=4f89dab3d788890003000096


This is the error I get

get_class_average([alice]) resulted in an error: unsupported operand type(s) for +: 'int' and 'str'


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 get_average(numbers):
    total=sum(numbers)
    total=float(total)
    result=total/len(numbers)
    return result

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"

def get_class_average(students):
    results=[]
    for student in students:
        get_average(student)
        results.append(get_average(student))
    average=sum(results)/len(results)
    return average


#2

here:

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

this function like the average you build in this exercise, and then you renamed average to get_average, so now your get_average is missing

please revisit the exercises so all the functions are right and have the right name


#3

Hi there,

It still shows the same thing even after the necessary changes and above code doesn't seem to work


#4

well, difficult to tell why if you don't show me the changes made


#7

A post was split to a new topic: Part of a while


#8

The answer is much simpler than it seems but you need to go back to the previous exercises, recalling functions and variables already programmed. Also, if you follow the instructions of the exercise carefully, you don't need the average function programmed again, just to call it at the end. So, you need the average of the class, the average for each student get_average(student), populate the empty results[] list, and append everything. This works:

def get_class_average(students):
    results = []
    for student in students:
        get_average(student)
        total = results.append(get_average(student))
    return average(results)

How is everyone doing?
#9

2 posts were split to a new topic: Part of a whole


#10

It isn't working for me
I tried the above code and checked if all the variables are according to the instruction. Same error message is shown


#11

I had changed average to get_average and vice versa


#12

Yep, your functions got a little mixed up, did you fix it?


#13

Not sure if you’ve fixed it or not, but I don’t think you understood what stetim94 said earlier.

You should have two functions; average() and get_average(), for whatever reason, you have just renamed average() to get_average() so you’re missing a function.

You need to rename get_average() to average() and then write a function called get_average() which calls average().

You need to follow https://www.codecademy.com/en/courses/python-beginner-en-qzsCL/1/1?curriculum_id=4f89dab3d788890003000096 and https://www.codecademy.com/en/courses/python-beginner-en-qzsCL/1/2?curriculum_id=4f89dab3d788890003000096 again.


#15

https://www.codecademy.com/courses/learn-python/lessons/student-becomes-the-teacher/exercises/part-of-the-whole
Thanks for the advice. I have done the correction yet I’m getting the error.

get_class_average([alice]) resulted in an error: ‘NoneType’ object has no attribute ‘getitem

What does this mean?


#16

You really need to post your updated code.


#17

This is what it should be right?


#18

no code included, please copy paste your code to the forum, the exercise url checks for username and load that users code


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

def get_letter_grade(score):
  score = float or int
  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))

def get_class_average(class_list):
  results=[]
  for student in class_list:
    get_average(results.append(student))
    return average(results)

#20

your get_class_average doesn’t calculate the correct class average

by default a function returns None at the end of the function, if we want to return something else at the end of the function we can use the return keyword, but it causes the function to end

so, your function ends in the first iteration of the loop, but to get the correct class average for a class with two or more students, the loop need to make all its iterations, so we should return the average(results) after the loop


#21

Does that mean that we have to return average(results) out side the loop?


#22

very good :smiley:


#23

I’m getting the same error