Scrabble_score - For loop only working once


#1

I've already seen the correct code for this, but I don't get why my own code is wrong and what I should change in it.

def scrabble_score(word):
    points = 0
    word.lower()
    for key in score:
        for i in range(0, len(word)):
            if word[i] == key:
                points += score[key]
            else:
                points += 0
    return points

Apparently, the for loop works only once, as I'm getting the following error message:

Oops, try again. Your function fails on scrabble_score("DuNe"). It returns "2" when it should return "5".

I know I could make this simpler, but I would like to know what's wrong with my code that it stops looping after the first time. I've been getting the same error on my previous codes. For some reason it won't continue looping despite the count not having reached the length of the variable.


#2

Your word.lower() is essentially useless here, since you did not save it under a variable.

word = "DINO"
word.lower()

print word #prints DINO

It seems that fixing that issue solved the problem.


#3

Here is the simple code which you can find easy to understand and trace the code.

def scrabble_score(word):
    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}
    word=word.lower()
    si = len(word)
    sum = 0
    j = 0
    while si > 0:
        i = score[word[j]]
        sum = sum + i
        j+=1
        si-=1
    return sum
print scrabble_score("PyThOn")

output :- 14


#4

Thank you! It worked now.
I still don't get why my old code doesn't work though.


#5

Explained it on top. It's because your word DuNe has both upper and lower cases, yet when you pass in word into the if statement, it checks if any key in score has the capital D, when in fact it only has the lower case form. That is why the loop does not work.

Just save word into word.lower() and it will solve the issue.


#6

Actually my friend ,your for loop 1st finds the 1st alphabet and returns appropriate value and later it doesn't start from beginning it continues from where the previous loop was stopped so, use while before for so if u find the value of 1st alphabet it should loop from beginning .Hope you now got the point why there was error


#7

Here is a simple conversion of the each score to a list and then using the sum function to add up all the elements.

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):
list=[]
if word.lower()=="":
print ("Enter a word")
else:
for i in word.lower():
list.append(score[i])

    return sum(list)

print(scrabble_score("Thisisrandom"))


#8

Try this, I found it to be a simplistic piece of code with no need to mess around too much with indecies!
The idea is a for loop, initiallizing a total value of zero, and then taking the values in the dictionary and adding them to the total!

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):
total = 0
for char in word.lower():
total += score[char]
return total

print scrabble_score("holymolly")


#10

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