7/9 Sending a Letter (Questions)


Hello everyone, I just get stuck in 7 exercise:

I should say that my code works, but there are some questions to make it better...

  1. How to check if argument is number when I define function?
    def get_letter_grade(score):
    ====if score % 1 = 0:
    Is this the only one way to check if arg is int? I hope there's another one more logical for ex
    def get_letter_grade(IT_IS_MUST_BE_INT_BRO(score)):
    Have Python got something like this?
  2. My code prints "This is not int, bro" when I ask it to calculate letter_grade. Just take a look:

def average(numbers):
total = sum(numbers)
total = float(total)
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.1 + quizzes * 0.3 + tests * 0.6

def get_letter_grade(score):
if score % 1 == 0:
if score >= 90:
return "A"
elif score >= 80:
return "B"
elif score >= 70:
return "C"
elif score >= 60:
return "D"
return "F"
return "This isn't int, bro..."

print (get_letter_grade(get_average(lloyd)))

But why?? It has input number, and %1 should == 0. Then it should check if n >= 90 etc. Why it returns else statement?


We are working with floats, not integers, for starters. It is not expected that we check the status of the parameter variable but you can check it with,

    from math import isnan
    # . . .
    if math.isnan(score):
        return "Data Error: Not a Number"


OK, but instructions are: "Expect score to be a number."
Or I didn't understand correctly?

btw I tried:
from math import isnan
if (math.isnan(score)):
File "python", line 36, in get_letter_grade
NameError: global name 'math' is not defined


Just tested and no problem. Be sure to import on the FIRST line of code, above everything else.

This is suggesting that we plan to work with a number. We haven't been introduced to the math module at this point (or have we?) so the author would not expect us to include it.


Okay, I tested to see if the SCT would balk at the extra code, and it didn't. However, Python is not too pleased since it is expecting a float and throws an exception when anything else is passed to the isnan method. So apparently I spoke too soon and was going with the assumption that isnan worked similar to JavaScript's isNaN(). Bad assumption, on my part.

For a better approach, http://stackoverflow.com/questions/8507509/numpy-using-isnanx seems bent on try: except.