Scrabble Project: Error with appending letter.lower() to letters list

For the Scrabble Project/Python 3:

When attempting the last portion of part 15, where we are to make our letter_to_points dictionary able to handle lower case inputs as well, I attempted to do so and encountered an error without a Traceback.

What I did:

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]

for letter in letters:

  letter.append(letter.lower())

points *= 2

print(letters)

print(points)

After running, nothing happens leaving me confused. There’s nothing on the console, its just completely empty and the 2 print statements don’t even run.

Would appreciate if someone could help me on this.

Firstly, welcome to the forums! I hope you’re doing well and it’s good to have you here.

I see a couple of issues here. Firstly, you have letter.append() in the for loop, however letters is the list and so you should be calling letters.append(), just a simple typo!

The second issue is that you are actually causing an infinite loop. The for loop checks the list every time a new letter is called. As such what is happening is that you’re loading in the list of ‘A’ to ‘Z’, then appending ‘a’ to the list, and then loading the new list in which is now ‘A’ to ‘a’. This continues indefinitely as the loop can never reach the end of the list and so letters keep coming, hence why nothing is being printed to the console.

Instead you can do this in a few ways. The way I chose to do it was to always convert the users input to an upper case letter, that way we can just use the original lists with no problem, and can handle lower and upper case letters. Another way would be to append the letters to a new list, as to avoid the problem of the list infinitely growing. There’s likely even more ways to do it, however I’ll leave that for you to decide which you’d prefer!

1 Like

Thank you so much for the reply!

I totally didn’t realize that adding onto a list that you’re iterating over will cause an infinite loop :rofl:

Although, I don’t really understand this part. In what way does converting input to uppercase allow us to use the original lists with no problem, as well as handle lower and uppercase characters? Sorry, a bit confused here.

So the original list is made up entirely of the uppercase letters, which is the list that you’re comparing to. So if you want your program to be able to take lowercase inputs, you can either

  1. Alter your original list to allow searching for both lower and uppercase letters or;
  2. Alter your code so that the user input gives you what you want

I prefer 2 as it feels more robust to me. The way you would do this, the way I was referring to, would be to convert the user input to uppercase before searching for it in the letters list by using str.upper(). So that way, it doesn’t matter if the user enters ‘ZAP’, ‘zap’, ‘Zap’ etc, as it’s all converted to ‘ZAP’, and then your original letters list of all uppercase can be used to get the score no problem. Example code below:

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]

user_word = 'zap'
user_word2 = 'ZaP'
user_word3 = 'ZAP'

new_word = user_word.upper()
new_word2 = user_word2.upper()
new_word3 = user_word3.upper()
# Takes the value of ZAP

print(score_word(new_word))
# prints 14
print(score_word(new_word2))
# prints 14
print(score_word(new_word3))
# prints 14

Thanks for the reply!I was originally confused by what you were referring to when you mentioned input, but I understand it now. Maybe its because I’m used to doing what the lesson requires of me, but I’ve never really thought of modifying the word itself.

Thanks for all the help! :smile:

1 Like