What does none mean?


#1

When I use this code, I see “83.8666666667
B
None” in the console. The first two lines are correct, but where is the “none” coming from?

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)
  return total/len(numbers)

def get_average(student):
  homework = average(student["homework"])
  quizzes = average(student["quizzes"])
  tests = average(student["tests"])
  score = 0.1 * homework + 0.3 * quizzes + 0.6 * tests
  return score

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

def get_class_average(class_list):
  results = []
  for student in class_list:
    student_avg = get_average(student)
    results.append(student_avg)
  return average(results)

students = [alice, lloyd, tyler]
class_avg = get_class_average(students)
print class_avg
print get_letter_grade(class_avg)

#2

In your case, so long as the correct results are being printed, it may be ignored (assuming you pass). It is the final print statement output, being that nothing was returned from the last called function (get_letter_grade()). You are printing what the interpreter is able to make out of this. None.

It might be more suitable to return a letter grade rather than print it. The function does say, get_, after all.

Try changing the prints to returns and see what happens.


Going back to your question, ‘What does None mean?’, we need to dig a little deeper into this representation. None is not a value. It has no boolean equivalent.

>>> def foo():
    pass

>>> print (foo())
None
>>> print (foo() and False)
None
>>> print (foo() and True)
None
>>> print (foo() or False)
False
>>> None and True
>>> None or True
True
>>> 

Notice what printed for None and True? Nothing.


#3

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