Quick Question about the def functions used in 7.Sending a Letter


#1


Replace this line with your 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 = sum(numbers)
    total = float(total)
    noob = total / len(numbers)
    return noob

def get_average(student):
    homework = average(student["homework"])
    quizzes = average(student["quizzes"]) 
    tests = average(student["tests"]) 
    sumb = homework * 0.10 + quizzes * 0.30 + tests * 0.60
    return sumb
    
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(lloyd)

Hi guys! New to programming and just had a question about the def functions in this exercise. (7. Sending a Letter).
The code does what it is supposed to do, but I'd like an explanation on how it works.

  1. If I've not called average(numbers) and get_average(students), why are they still being used for my get_letter_grade function? (get_letter_grade(lloyd) the only one I called, during the print statement.)

  2. How does get_letter_grade(lloyd) know to use the result from the get_average function? Can you explain whats going on step by step please?

Sorry if this is a noob question, but I'd like some help please.
Thanks in advance :slight_smile:


#2

Letter grade is an artifact of the grading process. We cannot assign it to a computational value, only derive it from one. We need a weighted average to work from.


#5

I don't understand. Could you put it in simpler terms for me and explain the questions I asked. Sorry, but I haven't been able to find the answer.

Thanks :slight_smile:


#6

@scriptslayer43157
Try to use following 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 = sum(numbers)
    total = float(total)
    noob = total / len(numbers)
    return noob

def get_average(student):
    homework = average(student["homework"])
    quizzes = average(student["quizzes"]) 
    tests = average(student["tests"]) 
    sumb = homework * 0.10 + quizzes * 0.30 + tests * 0.60
    return sumb
    
def get_letter_grade(score):
    print score
    print score>1000000000000000000000000000000000
    if score >= 90:
        return "A"
    elif score >= 80:
        return "B"
    elif score >= 70:
        return "C"
    elif score >= 60:
        return "D"
    else:
        return "F"
print "============ Bad ========="        
print get_letter_grade(lloyd)
print "============ Good ========="        
print get_letter_grade(get_average(lloyd))