Scrabble_score error '0' why?


#1

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

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

I expected to have my result in the (end) variable I tried everything



def scrabble_score(word):
  word1=word.lower()
  lenk=len(word1)-1
  end=0
  for i in range(0,lenk):
    if word1[i]==score[i]:
       end+=score[i]
  return end 



#2

No need to set up a conditional. Just query the lookup table.

end += score[i]

Not sure I like the choice of variable name. It does not reflect its true meaning. total would be more to the point. Truthfulness and meaningfulness are the two most important aspects of a variable name.


#3

I put the conditional for the program to be sure for no other character but the problem is still there I get the same error

def scrabble_score(word):
word1=word.lower()
lenk=len(word1)-1
total=0
for i in range(0,lenk):
total+=score[i]
return total


#4

The range() function excludes the last index.

for i in range(len(word)):

Your code is omitting the last letter in the word.

total += score[word[i]]

Another simpler way would be to loop over the word directly…

for letter in word:
    total += score[letter]

Sorry for the misinformation, earlier.


#5

Thank you for your reply but if you can help me to understand please. The code was correct and run with total += score[ word[i] ] and didn’t run with total += score[ i ] why is that. I appreciate the information about the range()I thought that it didn’t excludes the last index.


#6

When we iterate over a range, we are working with the index. When we iterate over an iterable (string, list, dictionary, tuple, set) we are working with the value.

for i in range(len(word)):

gives us an index which we must apply to word to get the value (letter) stored there.

for letter in word:

gives us a letter in the word.


#7

ok thank you very much.


#8

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