Lowercase? Help with python 2 exercise!


Hi! So the instructions of the scrabble exercise (in python 2 “practice makes perfect”) state that the function should work even if the letters you get are uppercase, lowercase, or a mix. But in the solution, codeacademy has word = word.lower(). Wouldn’t this only work for lowercase letters?

Your function should work even if the letters you get are uppercase, lowercase, or a mix.

The specification wants the scrabble_score function to work properly for all of the following arguments,

# lowercase argument
# 8
# uppercase argument
# 8
# mixed case argument
# 8 

The statement

word = word.lower()

fulfills that requirement.

The keys in the score dictionary are lowercase letters. By converting the argument to lowercase within the function, the letters of the argument can be looked up in the score dictionary and scored appropriately.

The intent of the specification is that we don’t have control over the argument. The user may provide a lowercase or uppercase or mixed case argument. We do have control over the function though and we want our function to be flexible enough to handle these arguments correctly. word = word.lower() helps us meet that specification by formatting the argument as we desire (within our function) so that we can score the word correctly. Without this statement, we would have to edit the score dictionary and create uppercase letter keys as well. For Example,

score = {"a": 1, "A": 1, "b": 3, "B": 3, ...}

That is unnecessary duplication. Additionally, we are not supposed to change the score dictionary. We are supposed to construct our function in a way that it works correctly with the existing score dictionary.

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