# Exercise-related question about in-build function .lower()

Hello everyone,

I am working my way through the “Build Chatbots with Python” skill path and I just completed the exercise about the Scrabble boardgame.

It has 3 extended exercises to try out and I have question about the last one which ask to modify the program so it can handle lowercase letters:

• As you can in my code below, I just added the following line of code in the `score_word` function: `letter = letter.upper()` with the reasoning of convert all lowercase letters to uppercase before it continues.
# create a list for letters 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"] # create a list for corresponding values of each letter 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] # make a dictionary by combining the 2 lists above with letter as key and letter value as value letter_to_points = { key: value for key, value in zip(letters, points) } # add blank tile with 0 value letter_to_points[" "] = 0 def score_word(word): """A function to calculate the score of each word.""" point_total = 0 for letter in word: letter = letter.upper() # this is the part I have the question for! point_total += letter_to_points.get(letter, 0) return point_total # create a dictionary with the player as the key and his list of words as the value player_to_words = { "player1": ["BLUE", "Tennis", "EXIT"], "wordNerd": ["earth", "EYES", "MACHINE"], "Lexi Con": ["ERASER", "BELLY", "HUSKY"], "Prof Reader": ["ZAP", "COMA", "PERIOD"], } # create an empty dictionary player_to_points = {} # extended exercise A def update_point_total(): for player, words in player_to_words.items(): player_points = 0 for word in words: player_points += score_word(word) player_to_points[player] = player_points # call function to update totals update_point_total() # print scoreboard print(player_to_points) # extended exercise 2 def play_word(player, word): """Add a new word that has been player to the list of words.""" player_to_words[player].append(word) # test the function play_word("player1", "CODE") print(player_to_words)
• In the video walkthrough which I then checked how the programmer solved it, he did the following:
letters += [ letter.lower() for letter in letters ] points *= 2

As far as I understand, he used a list comprenhesion to convert and add the lowercase letters. What is the point of his last line of code `points *=2`? He explained it in the video that we need to add corresponding values to the newly added lowercase letter, but for some reason my brain cannot process how this works here! Any help would be much appreciated!

P.S. It might be the case that I need more coffee!

Morning.
You’re converting the word to upper and then looking for it in the letters list. They’re leaving the word as lower, but doubling the length of the list to include all the lower case letters.
Both valid. I think I did it similarly to you.
What they’re doing in the first bit is adding the lower case letters onto the ‘letters’ string, i.e. doubling its length.
i.e. it becomes
[‘A’, ‘B’, 'C, … ., ‘x’, ‘y’,‘z’]
so, the points list is going to need to be extended to, so that it marries up, and each character has a point value.
‘points *= 2’ means take points and multiply it by 2 (i.e. the same as points = points + points)
Which, in the case of a list, means repeating it.
i.e.
[1, 2, 3, 4, 5] *= 2
would become
[1, 2, 3, 4, 5, 1, 2, 3, 4, 5]

Hopefully, that makes a little sense - I’m a little caffeine deprived myself.

2 Likes

Hello @pluginmaybe ,

‘points *= 2’ means take points and multiply it by 2 (i.e. the same as points = points + points)
Which, in the case of a list, means repeating it.
i.e.
[1, 2, 3, 4, 5] *= 2
would become
[1, 2, 3, 4, 5, 1, 2, 3, 4, 5]

Thanks for that, it made it very clear!

For some reason when I saw the multiplication symbol my mind thought that the points were doubled in a literal sense like 1 → 2, 2 → 4, etc. I thought for a moment that I was missing a rule of Scrabble or something regarding different values for lower and upper case letters!

But it is a list that gots multiplied and not a value so it is just copies the whole list as it is.

Thanks again!

1 Like