7. Sending a letter -- why score?


#1



I got my code to work, but do not know WHY it works. Specifically, in the last part:


def get_letter_grade(score):
    if score >= 90: 
        return "A"
    elif score >= 80 and score < 90:
        return "B"
    elif score >= 70 and score < 80:
        return "C"
    elif score >= 60 and score < 70:
        return "D"
    else:
        return "F"

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]
}

def average (numbers):
    total = sum(numbers)
    total = float(total)
    total /= len(numbers)
    return total
    
def get_average(student):
    homework = average(student["homework"]) 
    quizzes = average(student["quizzes"]) 
    tests = average(student["tests"]) 
    return homework *0.10 + quizzes*0.30 + tests*0.60
    
def get_letter_grade(score):
    if score >= 90: 
        return "A"
    elif score >= 80 and score < 90:
        return "B"
    elif score >= 70 and score < 80:
        return "C"
    elif score >= 60 and score < 70:
        return "D"
    else:
        return "F"
    
print get_letter_grade(get_average(lloyd))

how does the program know what "score" is? It was not defined in any of the prior functions, so how does it know what to get? Full code is below, but this is really bugging me -- what am I missing?


#2

in a way it doesn't. score is the function parameter and serves as a placeholder until you call the function. When you call the function you provide an argument to satisfy the parameter, for example:

def get_letter_grade(score):
    if score >= 90: 
        return "A"
    elif score >= 80 and score < 90:
        return "B"
    elif score >= 70 and score < 80:
        return "C"
    elif score >= 60 and score < 70:
        return "D"
    else:
        return "F"
print get_letter_grade(85)

then the argument (85) is copied into function parameter


#3

Thank you for the timely response!
I get that it inserts the value pulled from the student (Lloyd in this case) -- but how does it know to even go to that student? I guess I am wondering how it know that "score" should link up with "get_average".

Maybe I just need to write some more code along these lines to get a more intuitive grasp -- was just hoping there was a very simple explanation I could get :slight_smile:


#4

if you want to get the letter_grade of lloyd you would need to do:

get_letter_grade(get_average(lloyd))

first you call get_average which will give you the average of lloyds grade, then you can call get_letter_grade to convert this number into a letter/letter score


#5

Why does this print

B
None


#6

because B is the letter grade for lloyd


#7

Oh
:wink: :slight_smile: LOL Why am I... NVM


#8

Also I believe the if can be only this and it works:

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"

Cause it consider every condition in order :slight_smile:


#9

only this what?

Yep, the conditions are evaluated in order


#11