7. Sending a letter


#1



Oops, try again. get_letter_grade(89) returned None instead of 'B' as expected.


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)
    total /= len(numbers)
    return total
    
def get_average(student):
    homework = average(student["homework"]) * 0.10
    quizzes = average(student["quizzes"]) * 0.30
    tests = average(student["tests"]) * 0.60    
    return homework + quizzes + tests
    
def get_letter_grade(score):
    score = int
    if score >= 90: 
        return "A"
    elif score >= 80:
        return "B"
    elif score >= 70:
        return "C"
    elif score >= 60:
        return "D"
    else:
        return "E"
    
    print get_letter_grade(get_average(lloyd))


#2

Hi, @astylianou1989 ,

This statement does not accomplish what you probably intended ...

score = int

To find out what it actually does, insert a temporary print statement after it ...

print score

#3

still receive the same error. Any other hints :slight_smile: ?


#4

To perform the conversion, you need to do this ...

score = int(score)

However, you do not need to convert score to an int for the get_letter_grade function to work.

This line should not be indented, otherwise it is considered to be part of the get_letter_grade function ...

print get_letter_grade(get_average(lloyd))

Should this ...

return "E"

... be ...

return "F"

?


#5

Thanks for this. Can i ask, when expecting score to be a number, why do we need to add '(score)'?

My brain still has not understood the difference between:

score = int VS score = int(score)


#6

int is a type of object in Python that represents whole numbers. 1, 7, and 10 are examples. In fact, if you execute this ...

print type(11)

You get this output ...

<type 'int'>

int is also a Python built-in function. You can use it to convert a float or a str object to an int, but to do so, you must call the function, for example ...

x = 3.14159
y = int(x)
print x
print y

Output ...

3.14159
3

Note from the output that x is still 3.14159, but 3 got assigned to y.

If you do this with the intent of converting x to an int ...

x = 3.1415
x = int
print x

... you will see this output ...

<type 'int'>

However, this does successfully convert x to an int ...

x = 3.1415
x = int(x)
print x

Output ...

3

#7

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