Scrabble_score


#1

https://www.codecademy.com/courses/learn-python/lessons/practice-makes-perfect/exercises/scrabblescore?action=lesson_resume

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}

print score["z"]
total =[]

def scrabble_score(word):
  for letter in word:
    for char in score:
      if char == letter:
        score[char]
        total.append(score[char])
    	return sum(total)
    

I am having a lot of trouble trying to figure out what is wrong with my code. The error says "Your function fails on scrabble_score(“pie”). It returns “4” when it should return “5.” Is is because my code stops adding at i rather than e? If this is the case what do I have to do in order for the code to complete the total?


#2

a function ends the moment a return keyword is reached, so we want to return the sum after the loop(s), so the loop can make all there iterations rather then being ended to early by return keyword


#3

I figured it out!

         "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):
    word = word.lower()
    total = 0
    for letter in word:
        for leter in score:
            if letter == leter:
                total = total + score[leter]
    return total
  
print scrabble_score("DuNe") 

I took your advice and moved “return total” out. For a while I was having trouble with uppercase letters and realized i forgot to lowercase the string. At first I had only “word.lower()” I then had to change it to “word = word.lower()” and it worked!


#4