Student Becomes the teacher: It's Okay to be Average


#1

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


Keeps giving this error when before it kept giving the error for " total = sum(numbers)", but now, it just keeps spitting this error :frowning:
Oops, try again. average([3, 0]) resulted in an error: float() argument must be a string or a number


Expect nothing to print, but it should work.

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

#2

you should first get the sum of numbers and then convert to float (this is also the order of the instructions)


#3

It worked. Thank you. :blush:
It does not make sense to me, since the marks are already a float, why do we need to declare total to be a float? From the description in the exercise: float/int = float


#4

you will use average() later in your program, you want to be absolutely certain integer division (which cause the number to be floored) does not occur


#5

But does the fact that they are already floats mean that they won't be floored?


#6

i think the example in the lesson:

5 / 2
# 2

5.0 / 2
# 2.5

float(5) / 2
# 2.5

explains this nicely?


#7

Ok, now it is giving the error: File "python", line 22, in average
Which is this line: total = sum(numbers)


#8

can i see your full code?


#9

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

def get_average(student):
    homework = average(student["homework"])
    quiz = average(student["quizzes"])
    students = average(student["tests"])
    h_mark = homework * 0.1
    q_mark = quizzes * 0.3
    t_mark = tests * 0.6
    grade = h_mark + q_mark + t_mark
    return grade

#10

look at this two lines:

quiz = average(student["quizzes"])
q_mark = quizzes * 0.3

notice anything about them? quizzes is a undefined variable


#11

I don't get how it is undefined... Is it not defined in:
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]
}

?


#12

as key in the dictionary yes, but that is not the points. Its purely about this lines of code, you create a variable (quiz) to store a students quiz grade, then you need to calculate the weighted average, and then all of sudden you use quizzes, while you stored the grade in quiz


#13

Ohhh!! I see what you are saying. Yes!!
Now after changing "q_mark = quizzes * 0.3" to "q_mark = quiz * 0.3", it returns the error "Oops, try again. get_average(alice) raised the following error: can't multiply sequence by non-int of type 'float'"


#14

you have the exact same problem for tests:

students = average(student["tests"])
t_mark = tests * 0.6

#15

Changed students to test and now it is still hating on me
"Oops, try again.
get_average(alice) returned 0.0 instead of the expected 91.15"


#16

Ok, I now literally have no clue what is going on now...

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

error : get_class_average([alice, lloyd]) returned 91.15 instead of 85.85 as expected
"


#17