Scrabble (Dictionaries)

constructive criticism requested with actually reviewing the source.
I will not move on from a problem until I understand what happened and why my code that didn’t work failed to do so. after 8 hours your ‘usual suspects’ when it comes to debugging I dont think are applicable. I implemented no complicated code until all alternatives were exhausted. I at that point already had a workaround with code I understood. My only reason for posting is an attempt to understand what went wrong.

moving on:
inside the loop to separate the words from:
([[word from word in words]from words, player in{ words played}] ~~referencing [words previously played]
I put my function taking inputs 'score_word(player, word) logically with the little ive learned to date I visualize a word, singular and player singular (since thats what I defined as entering the function) going through the score and add words process. add word takes add_word[player] += ‘word’, or with a previous line storing old data, then add_word[player]= both old and new.

Then the score in another loop(still in function) that separates letters and matches them to {letters to points} value to finally player_score[player] += new_score . A-o-k ezpz in my brain makes sense. new inputs as they’re looping will divvy it out only to defined areas.

however, what I found was rather than doing that it would (when the two separate functions were 1) assign a word to each referenced player or a letter depending on if dic[key]= or dic[key]+=

this confuses me. also irritates me because my primary goal when I approach every problem is flexibility (ability to adapt to unknown variables aka in this case a list opposed to word by word basis) and efficiency (least drag on server).

I cannot visualize how the assigning of new values (in some attempts del key new key/value) would produce the outputs stated. I tried changing variable ‘player’ inside and outside the loop/function to ‘players/playerzz/zz/x’ and word to ‘wxrd/wx/x’ many combinations thereof.

finally:
it was an adhoc incomplete setup I came up with for making flexible dictionaries, or lists. like how they ask you to create new dicts based off x-value in instructions. I realize, since I find myself using it a bit with the lessons I’m at in the program dictionaries, that it would need further adaptability given more strange formats of entry. I used to have a else: print(‘unacceptable command, additional pylons required’).

To view what I’m talking about simply route provided loop through the addword function which refs internally score.

copy past vaguely whats meant now that I’m home.

Summary
def score_word(word, player):
    word = word.upper()
    # words_played[player] += word
    # words_played[player].append(word) etc
    # lummox = words_played[player]
    # words_played[player] = lummox + word (With formatting obvi)
    for scrabletter in word:
        players_score[player] += letter_to_points.get(scrabletter)
    word_add(player, word)
    return player, word


for player, words in player_to_words.items():
    for word in words:
        score_word(word, player)



terminal == 
{'player1': , ['B', 'L', 'U', 'E']], ['T', 'E', 'N', 'N', 'I', 'S']], ['E', 'X', 'I', 'T']], 'wordNerd': [[[[], ['E', 'A', 'R', 'T', 'H']], ['E', 'Y', 'E', 'S']], ['M', 'A', 'C', 'H', 'I', 'N', 'E']], 'Lexi con': [[[[], ['E', 'R', 'A', 'S', 'E', 'R']], ['B', 
'E', 'L', 'L', 'Y']], ['H', 'U', 'S', 'K', 'Y']], 'Prof Reader': [[[[], ['Z', 'A', 'P']], ['C', 'O', 'M', 'A']], ['P', 'E', 'R', 'I', 'O', 'D']]}

{'player1': ['BLUE', 'TENNIS', 'EXIT', 'EARTH', 'EYES', 'MACHINE', 'ERASER', 'BELLY', 'HUSKY', 'ZAP', 'COMA', 'PERIOD'], 'wordNerd': ['BLUE', 'TENNIS', 'EXIT', 'EARTH', 'EYES', 'MACHINE', 'ERASER', 'BELLY', 'HUSKY', 'ZAP', 'COMA', 'PERIOD'], 'Lexi con': ['BLUE', 'TENNIS', 
'EXIT', 'EARTH', 'EYES', 'MACHINE', 'ERASER', 'BELLY', 'HUSKY', 'ZAP', 'COMA', 'PERIOD'], 'Prof Reader': ['BLUE', 'TENNIS', 'EXIT', 'EARTH', 'EYES', 'MACHINE', 'ERASER', 'BELLY', 'HUSKY', 'ZAP', 'COMA', 'PERIOD']}




well I fixed it on my own.

Summary
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]
wordsy = [['BLUE', 'TENNIS', 'EXIT'], ['EARTH', 'EYES', 'MACHINE'],
          ['ERASER', 'BELLY', 'HUSKY'], ['ZAP', 'COMA', 'PERIOD']]
players = ['player1', 'wordNerd', 'Lexi con', 'Prof Reader']
# dictionary maker


def newdict(things, value):
    if isinstance(value, int) or len(value) == 0:
        newdict = [value for _ in range(len(things))]
        return {n: y for n, y in zip(things, newdict)}
    else:
        return {n: y for n, y in zip(things, value)}


players_score = newdict(players, 0)
letter_to_points = newdict(letters, points)
player_to_words = newdict(players, wordsy)
words_played = newdict(players, [])
letter_to_points.update({" ": 0})


def score_word(word, player):
    word = word.upper()
    **words_played[player] = words_played[player] + [word]**
    for scrabletter in word:
        players_score[player] += letter_to_points.get(scrabletter)
    return player, word


for player, words in player_to_words.items():
    for word in words:
        score_word(word, player)

print(score_word('tiger', 'wordNerd'))
brownie_points = score_word('BrOwNiE', 'player1')



print('end of game:')
for player in players:

    print('''

                {} played: {}
                for a point total of {}.

    ''' .format(player, words_played[player], players_score[player]))

victor = {}
score = 0
for key, value in players_score.items():
    if value > score:
        score = value
        victor = {key: value}


print('''
and the victor is....

                            {}

long may they reign'''.format(victor))

print('''
and the victor is....

                                    o
                                   $""$o
                                  $"  $$
                                   $$$$
                                   o "$o
                                  o"  "$
             oo"$$$"  oo$"$ooo   o$    "$    ooo"$oo  $$$"o
o o o o    oo"  o"      "o    $$o$"     o o$""  o$      "$  "oo   o o o o
"$o   ""$$$"   $$         $      "   o   ""    o"         $   "o$$"    o$$
  ""o       o  $          $"       $$$$$       o          $  ooo     o""
     "o   $$$$o $o       o$        $$$$$"       $o        " $$$$   o"
      ""o $$$$o  oo o  o$"         $$$$$"        "o o o o"  "$$$  $
        "" "$"     """""            ""$"            """      """ "
         "oooooooooooooooooooooooooooooooooooooooooooooooooooooo$
          "$$$$"$$$$" $$$$$$$"$$$$$$ " "$$$$$"$$$$$$"  $$$""$$$$
           $$$oo$$$$   $$$$$$o$$$$$$o" $$$$$$$$$$$$$$ o$$$$o$$$"
           $"""""""""""""""""""""""""""""""""""""""""""""""""""$
           $"                                                 "$
           $"$"$"$"$"$"$"$"$"$"$"$"$"$"$"$"$"$"$"$"$"$"$"$"$"$"$

                            {}

long may they reign'''
      .format(victor))

A lookup dictionary is so much simpler to use, so,

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]
scores = dict(zip(letters, points))
    players_score[player] += scores.get(letter.upper(), 0)
1 Like

pretty sure thats what I had, unless I misunderstand you.
aka
{dict}[value]+={dict4score}.get(variable_letter)
unless you mean dropping the word.upper() from earlier and instead (variable_letter.upper)
which I like since I think that means it will only render each letter once and on lookup?
the bolded ‘get’ confused me.

Blame the editor for that; wasn’t my bolding.

My thinking is more along different lines.

entrants = ['player1', 'wordNerd', 'Lexi con', 'Prof Reader']
players = {n: {'score': 0, 'words': []} for n in entrants}
>>> from string import ascii_letters
>>> 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] * 2
>>> scores = dict(zip(list(ascii_letters), points))
>>> sum(map(lambda x: scores[x], list('QUIZZIFY')))
41
>>> 
1 Like