Type error at last line

letters = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
points = [1, 3, 3, 2, 1, 4, 2, 4, 1, 8, 5, 1, 3, 4, 1, 3, 10, 1, 1, 1, 1, 4, 4, 8, 4, 10]

letters_to_points = {letters:points for letters, points in zip(letters, points)}

letters_to_points[" "] = 0 

def score_word(word):
  point_total = 0
  for char in word:
    point_total += letters_to_points.get(char, 0)
  return print(point_total)
score_word("BROWNIE")

player_to_words = {"BLUE":["EARTH", "ERASER", "ZAP"], "TENNIS":["EYES", "BELLY", "COMA"], "EXIT":["MACHINE", "HUSKY", "PERIOD"]}

player_to_points = {}
for player,words in player_to_words.items():
  player_points = 0
  for word in words:
    player_points += score_word(word)

why lhe last line is TypeError: unsupported operand type(s) for +=: ‘int’ and ‘NoneType’ ???

here:

return print(point_total)

why do print()? Anyway, print returns None, as a result, score_word also returns None, which means you get a TypeError

1 Like

This line

player_points += score_word(word)

Is looking for the value returned by the function score_word(word) to be an int.

What does score_word return? Look here:

return print(point_total)
  • When Python sees that, the first thing it does is evaluate the statement to the right of return to see what to return. That statement is print(point_total)

  • So, as with any print() statement, the first thing that happens is that the value within the parentheses is evaluated, so that print() knows what to print. That value is point_total, which is an int taken from a dictionary, letters_to_points.

  • The print() statement dutifully prints that int, which means that a string representation of the int is sent to the standard i/o device, namely, your screen.

  • But return is still waiting for a value to be returned from print(). return is not print(); return is how Python expressions, functions and statements talk to each other, not to you.

  • And, bottom line (finally!), print() always returns None!

1 Like