9. scrabble_score


#1

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):
    txt = word.lower()
    k = []
    for i in range(0,len(txt),1):
        k.append(score[txt[i]])
    for j in k:
        s = 0
        s = s + k[j]
    return s

it is giving error like "Oops, try again. Remember to make your function work for upper- and lowercase letters!"


#2

A few issues:

Firstly, you should really refrain from using single character variables all over your code - this is bad practice makes it much harder for other people to debug.

Secondly, the problem in your code is that in your second for loop, s is being reset to 0 every time. The line s = 0 needs to be moved out of the loop.

Thirdly, another problem in your code is that k[j] means "take the value of j and use it as an index number for k" - but, since j (which is one of the letter scores) is actually being assigned the values inside k, you've mixed up what you're trying to do (this would be much easier to explain with better variable names).

The exercise tests this using "ABC". In this case k would equal [1, 3, 3] and the second iteration of the loop would look like this:

s = s + k[3]

Therefore, an error would be thrown because the index values of k are 0, 1 and 2. Since it fails on "ABC", it (wrongly) assumes it's because you haven't accounted for uppercase letters.


#3

thank you so much for your all suggestions
i will keep those in mind :slight_smile:

btw i have use string.lower() command in the starting
so it should be converted in the lowercase
then there isn't any question about uppercase


#6

No problem!

You did indeed deal with uppercase, which is why I had to check the exercise's SCT to determine how it was checking your code.

Another tip is that x = x + y can be shortened to x += y


#7

yp actually was done at that time when u suggested me
thank you :slight_smile:


#10