0

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.

  • The function is called scrabble_word
  • It has two parameters: pointtable, a dictionary containing the point values of all of the letters, and word the actual word: a list of letters
  • Your objective is to return the sum of the point values of the letters."

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?

4
  • What's the problem? This isn't very clear. Commented Mar 13, 2018 at 18:37
  • "It was wrong". That almost as unhelpful as "It didn't work". Describe what was wrong, what the output was and why it was not what you desired. Commented Mar 13, 2018 at 18:38
  • 1
    Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation. On topic and how to ask apply here. Commented Mar 13, 2018 at 18:41
  • for i in range(len(word)) --> what makes you think this i is going to correspond to the right letter in pointtable? First get the letter, use console.log() to maker sure you have the right letter, then find that letter in pointtable, grab the associated points and sum Commented Mar 13, 2018 at 20:04

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.