How is Everybody Doing? Help


#1


I need help with "How is Everybody Doing?" part. I am having a hard time understanding the format and structure of Python. Can somebody please explain to me how to get the right answer, but tell me why mine is not working?

I am getting the error:

"Oops, try again. Did you remember to print the average of the class? It looks like "83.8666666667" wasn't printed."

Here is my code:

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 = float(sum(numbers))
    total = total / len(numbers)
    return total
    
def get_average(student):
    homework = average(student["homework"])
    quizzes = average(student["quizzes"])
    tests = average(student["tests"])
    return .10 * homework + 0.3 * quizzes + .6 * tests
    
def get_letter_grade(score = 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))

students = [lloyd, alice, tyler]

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

#2

you need two function calls, one to print the class average and one to print the letter grade of the class average. You currently only have the function call which prints the letter grade, at the function call which prints the class_average

Also, make sure both function calls out outside/after the get_class_average function


#3

That was it. All I had to do was remove the indentation and change my code up a bit. I think I had my code right before I posted the help, but I am assuming that my print command was still inside of the function. I guess I am having a hard time understanding the syntax/formatting of Python.

Here is what I did to fix it. Thank you for your help!

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

#4

by removing the indention you placed the function calls outside the function.

the print statements will make sure that what is returned by the function is printed. Hopes that clarifies something

return literally means the function returns something to you (hands you something back), we use print to print whatever was handed back to use to the output


#5

If I were to remove the return average(results) and tried using my 2 print statements, it would error correct? Because it does not know what to print because it was not "returned" to my pc?

Thank you for all of the help.


#6

by default a function would return None, so it would print None. By using the return keyword you change what the function returns


#7

That makes sense. Thank you for clarifying for me.

Do you know PowerShell? I am trying to see the difference between functions in PowerShell vs Python. From my understanding in PowerShell, we use functions in PS for code that we are going to use over and over. From the exercise, it seems function are different.


#8

i don't know powershell, i try to avoid windows whenever possible

in python you can also use a function multiply times:

def add(a,b):
    return a + b

print add(5,3)
print add(2,6)
print add(9, 12)

i now execute the function three times


#9

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