9/15 Scrabble (confused about "return"


#1

I originally wrote:

def scrabble_score(word):
  points = 0
  for letter in word.lower():
    points += score[letter]
    return points

Which left me with the Codecademy error:

Your function fails on scrabble_score(“pie”). It returns “3” when it should return “5”.

. When I changed it to move “return points” back a tab it worked just fine, like so:

def scrabble_score(word):
  points = 0
  for letter in word.lower():
    points += score[letter]
  return points

I’m confused why the first one resulted in a problem, but the second did not.


#2

By default, a function returns None at the end of the function. If we want to return something else at the end of the function, we can use the return keyword.

Given return is the last thing a function does, when a return keyword is reached, the function ends


#3

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