werlless
werlless

Reputation: 49

Scrable game code in python

I've been working on some exercises, and have run into a problem with one of them.

The prompt:

"Scrabble is a board game, where a word is worth as many points as the sum of its letters. Write a function that calculates the point value of a word.

I saw an example where we find the sum of values of dict so i tried to do it that way:

  def scrabble_word(pointtable, word):
   s=0 
     for k in word: 
      s += word[k] 
    return s

It was wrong, then I realized the word is a list, and changed it a bit:

    def scrabble_word(pointtable, word):
    s=0 
      for i in range(len(word)): 
        s += pointtable[i] 
      return s

It still wrong. Should I define the values first? Can you help me?

Upvotes: 0

Views: 487

Answers (1)

chepner
chepner

Reputation: 530960

Assuming pointtable is a dict like {'e': 1, 'q': 8, ...}, then you want to use items from word as the index into pointtable.

def scrabble_word_score(pointtable, word):
    score = 0
    for letter in word:
        score += pointtable[letter]
    return score

Using more descriptive variable names can help you notice expressions that don't make sense.

Upvotes: 1

Related Questions