Scwabble score


Oops, try again. Does your scrabble_score function take exactly one argument (a string)? Your code threw a "unsupported operand type(s) for +=: 'int' and 'NoneType'" error.

The blooming thing to work.

Replace this line with your code. 
def scrabble_score(word):
    word = str(word)
    final = 0
    for s in score:
        if s in word:
            for s in range(0,len(word)):
            return final


Well. Like it says. You can't add None to an int

Unless you meant to do that, then it would seem like you've mixed up your values and would need to reconsider where you're getting that value you're adding to an int

Also, note that if a loop exits on its first iteration, then it's not a loop at all. So you should probably either not have a loop there, or not be exiting the function at that time.


def scrabble_score(word):
word = str(word)
final = 0 # to hold the value
for letter in score: #cycle through keys in score dictionary
if letter in word: #if the keyis in the string
save=score.get[letter] #retrieve the value of the key
final+=save # add this value to final
return final

I came at it from a slightly different approach


3 posts were split to a new topic: Scrabble score


No idea why there's no 'points' for this solution..

def scrabble_score(word):
    final_score = 0
    for key in word:
        if key in score:
            final_score = final_score + score[key]
    return final_score

print scrabble_score(raw_input("what's your word").lower())

Any tips?
NVM... didnt like .lower() function on the last line - had to squeeze it within the function


byteplayer78569, you are using 2, for loops. both of them are using s as the index so that is never going to end well :slight_smile:.

Not sure of your logic even with that fixed. As far I can make out with my limited knowledge:

  • With your first for loop you are going through every letter in the score dictionary
  • The if is looking to see if the current letter is in the word.
  • Not really sure what the 2nd for is for but it looks like it aims to set up a count going from zero to 1 more than the end of the word. You are then using that number as an index to get values from the score dictionary but the index of that is letters not numbers so it will not be able to find a corresponding value for that index entry.

What you should be doing is going through each letter of the word.
You should be using each letter as an index to score adding the corresponding value of that letter to final (for example: if the letter is q it should add 10 to final).

You just need 1 for loop and no if statement.


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