Camilla Maia
Camilla Maia

Reputation: 23

TypeError: unsupported operand type(s) for +: 'int' and 'NoneType' when sum a list

I'm trying to write an average grades calculator in Python for CodeAcademy course. I keep getting this error

TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'

on the average function, total = sum(numbers). I understand that it means that I'm trying to sum two different types, which is not possible. But I don't understand where is this NoneType coming from, if I'm trying to sum just a list of numbers from a given dictionary?

Also, what would be a better way to write this code? I assume there are much simpler ways to do exactly the same.

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]  
}    
class_list = [lloyd, alice, tyler]  
students = [lloyd, alice, tyler]  

# Add your function below!


def average(numbers):    
  total = sum(numbers)  
  averege_grade =  float(total)/len(numbers)  
  return averege_grade  

def get_average(student):  
    homework = average(student["homework"])  
    quizzes = average(student["quizzes"])  
    tests = average(student["tests"])  
    total_average = float(homework) * 1 + float(quizzes) * 3 + float(tests) * 6  

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"  

def get_class_average(class_list):  
    results = []  
    for student in class_list:  
        resultadinho = get_average(student)  
        results.append(resultadinho)  
        return average(results)  
print get_class_average(students)  
print get_letter_grade 

Upvotes: 2

Views: 6979

Answers (1)

Jean-François Fabre
Jean-François Fabre

Reputation: 140168

you're not returning anything from get_average(), so when sum starts (using 0 as first accumulation value, it tries to add None and 0 which explains the error message.

Upvotes: 2

Related Questions