Scrabble_score


#1

What’s the problem with my code?

Does your scrabble_score function take exactly one argument (a string)? Your code threw a “‘bool’ object is not iterable” error.

score = {"a": 1, "c": 3, "b": 3, "e": 1, "d": 2, "g": 2, 
         "f": 4, "i": 1, "h": 4, "k": 5, "j": 8, "m": 3, 
         "l": 1, "o": 1, "n": 1, "q": 10, "p": 3, "s": 1, 
         "r": 1, "u": 1, "t": 1, "w": 4, "v": 4, "y": 4, 
         "x": 8, "z": 10}
def scrabble_score(word):
  result=0
  word=word.lower()
  word=word.isalpha()
  for char in word:
    result+=score[‘char‘]
  return result

#2

here:

word=word.isalpha()

isalpha() returns true or false depending on if word contains only alphanumeric charaters

for this exercise, you can assume all input will be valid. Get rid of isalpha (or implemenent it correctly with if statement if you really want to)


#3

I delete it but my code still doesn’y work.

Your code looks a bit off–it threw a “‘char’” error. Check the Hint if you need help!

score = {"a": 1, "c": 3, "b": 3, "e": 1, "d": 2, "g": 2, 
         "f": 4, "i": 1, "h": 4, "k": 5, "j": 8, "m": 3, 
         "l": 1, "o": 1, "n": 1, "q": 10, "p": 3, "s": 1, 
         "r": 1, "u": 1, "t": 1, "w": 4, "v": 4, "y": 4, 
         "x": 8, "z": 10}
def scrabble_score(word):
  result=0
  word=word.lower()
  for char in word:
    result+=score['char']
  return result

#4

here:

result+=score['char']

why is 'char' a string? the dictionary has no key 'char'


#5

Your answer is much appreciated.


#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.