8. Part of the whole


#1

This is my code:

and this is the error:

Would you please help guys, I have been trying to debug the error for a while and I am strggling to find the solution.


8. part of a whole
#2

I have checked your code, It does not have any error but other part of your code have some error that reflecting on get_class_average(students) function , You can see this function depends on other two funtions :
1. get_average(student)
2. average(numbers)

Pls post your full code here , thank you. :slight_smile:


#3

4 posts were split to a new topic: 8. part of a whole


#6

A post was merged into an existing topic: 8. part of a whole


#8

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)
    average = total/len(numbers)
    return average
def get_average(student):
    homework = average(student["homework"])
    quizzes = average(student["quizzes"])
    tests = average(student["tests"])
    return 0.1 * average(student["homework"]) + 0.3 * average(student["quizzes"]) + 0.6 * average
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)) 
def get_class_average(students):
    students = [lloyd,alice, tyler]
    results = []
    for student in students: 
        get_average(student)
        results.append(get_average(student))
        return get_average(results)

@greentreemee


#9

problem here:

return 0.1 * average(student["homework"]) + 0.3 * average(student["quizzes"]) + 0.6 * average

0.6 multiplied by function? sounds like a bad idea

furthermore, your students list should be outside the function. Don't overwrite the function parameter

finally, here:

return get_average(results)

you should return the average() of results.

also, make sure to place return outside the loop. A function ends the moment a return keyword is reached, you don't want your loop to break in its first iteration


#10

I will point where you made mistakes ,let you to hunt these bugs by your own!

Problem 1 ( already mentioned by mod)

Refer to this exercise again...
https://www.codecademy.com/en/courses/python-beginner-en-qzsCL/1/2

Once again ,lets go find meaning of variable and why we use it?

Lets see this example...

name = "motherm"

#If If I had to print your name's value again we can two things..


print (name)  #method1
# or 

print("motherm") #method2

#What method you would prefer ,1 or 2?

I will use method method 1, why ?
Method2 is same is like using name (Noun) of a person many number of times rather than using pronoun (you/he/she/it)...
using variable have these profits:

  1. You'll have no fear to loose your data (information)
  2. Your code is reusable.
  3. less bugs.
    and many more..

lets get another issue that your code have...

using student list inside the function (already mentioned by mod)

Lets look at why we use functions.
Functions are like reusable ideas , like a Swiss Army knife that can get fits for many problems of same nature

Lets understand what I meant by that...

lets see we made a small function that takes a number and checks if number is zero or non zero.

def checkZero(x):
    if x == 0:
        return "Number is zero"
    return "Number is not  zero"

Now why I called function a Swiss Army knife?
Because this special function can work for any number..

checkZero(9)  # It will return  "Number is not  zero"
checkZero(0)  # It will return  "Number is  zero"
checkZero(19)  # It will return  "Number is not  zero"
checkZero(12 % 3)  # It will return  "Number is  zero"

So your function can be used for many numbers!

What you did is , by putting
students = [lloyd,alice, tyler]..it will just work for one specific list.
.Made function less powerful!
If I put another list (students is a list only) and test, It will fail!
So define it outside!...

These are few minor mistakes that can be debugged after you solved those two mentioned by Mod!

I hope it makes your doubt a bit more clear! :slight_smile:


#11

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