Accomodating double/triple word/letter scores into Scrabble code in Dictionaries Project on Learn Python 3

I’ve been using the code that users arrive at after completing the Scrabble project in the Dictionaries module for some time to keep score when playing Scrabble in real life for a few months. I’ve been getting around the problem of accommodating double/triple word/letter scores by just typing the word/letter twice/thrice, but I think there must be a more sophisticated way of doing this using loops and functions. However if there is I haven’t been able to find a successful way of doing it- I’ve tried a few things that I quite soon realised were flawed ideas. My main issues are being unsure of where in the code to add this in, and how to get the function to apply to only one word at a time, and not apply to every subsequent word entered.

The code the user is left with after completing the initial project, which I’m looking to modify, is below. The square brackets content in players_to_words is not from Codecamedy’s instructions, but the current state of my attempt after getting too confused over trying to write new functions. Originally the square brackets just contain the words played, in individual strings. This case gives the correct answer of 37, but is clearly very cumbersome to write so probably takes longer to get an answer than doing the sum in my head, so I’m not very happy with it.

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, 1, 1, 3, 10, 1, 1, 1, 1, 4, 4, 8, 4, 10]

letters += [letters.lower() for letters in letters]
points *= 2

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

letters_to_points[" "]= 0

def score_word(word):
point_total = 0
for letters in word:
point_total += (letters_to_points.get(letters, 0))
return point_total

players_to_words = {“player”: [(“b” * 3 + “ox”) * 2, “a”]}

players_to_points = {}
def update_point_total():
for players, words in players_to_words.items():
player_points = 0
for word in words:
player_points += score_word(word)
players_to_points[players] = player_points

def play_word(player, word):
players_to_words[player].append(word)

update_point_total()
print(players_to_points)

Hi there

So if I get your question correctly you’re trying to get the value of a word and then multiply it by the word value?

If I understand your question correctly maybe something like this could get you started. If you would like to keep score for an entire game of scrabble you could try to wrap a function like the one below in a while loop.

The .index searches for the index in letters and gives the points back at the same index in points. The word_value is set to a default of one, but can be altered if you like.

An alternative to using two lists (letters and points), you can try creating a dictionary containing the letters as keys and points as values, and loop over that instead.

def calculate_points(word, word_value=1):
    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, 1, 1, 3, 10, 1, 1, 1, 1, 4, 4, 8, 4, 6] 
    total = 0

    for letter in word:
        if letter in letters:
            index = letters.index(letter)
            total += points[index]

    return total * word_value

Thank you for the tips! I thought I could get around the issue of only doubling the score of one word at a time (and not all subsequent words, as was happening when I put the words into the list in players_to_words) with the following code after the calculate_points function:

score_player_1 = (score_word(“cat”))
print(score_player_1)

score_player_2 = (score_word(“”))
print(score_player_2)

players = {“Player 1”: [5], “Player 2”: }
print(players)

So the score for one word at a time will appear in the terminal, and can be added to the relevant list of “players”. As the player puts down more words, the subsequent scores can be logged e.g [5 + 6 + 13].

I still wouldn’t expect anyone else to use this code as there’s still a lot of fiddly manual input (e.g. the previous player’s word has to be deleted before the next player’s can be inputted) but I’m a bit happier with it for now. Still, any suggestions for improvement of what I have to make it more streamlined would be welcome, as would any suggestions for how to accommodate the double/triple letter scores- I can’t currently think of a way to double or triple the score of a single letter in a word.

Apart from just entering the letter twice/trice :sweat_smile: ?

That’s what I’ve been doing so far.