Click here to Skip to main content
15,902,189 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is my code:


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):
    word = (word.lower())
    final_score = 0
    for key in score:
        if key in word:
            final_score = final_score + score[key]
    return(int(final_score))

print(scrabble_score("xenophobia"))


What I have tried:

I have tried removing the if statement and just using the for loop, I've also tried removing final_score.
Posted
Updated 29-Oct-22 12:16pm
Comments
Richard Deeming 31-Oct-22 6:36am    
NB: This appears to be an extremely over-simplified Scrabble score calculator. Aside from the fact that it ignores the bonus squares (double/triple word/letter), there is no way to know how many letters the user played, so no way of knowing whether to award them the bonus for playing all seven letters.

I can only hope this is part of a programming course, and the additional complexity will be introduced later. :)

Instead of removing things, you should have been walking through the code line by line, executing each statement in your head and probably keeping track of variable content on paper. That way you get a better understanding of the code instead of guessing at what's wrong.

The problem is your algorithm. You're taking each letter in the dictionary and seeing if it in the word when you should have been taking each letter in the word and looking it up in the dictionary. That's the entire point of the dictionary!
 
Share this answer
 
Comments
Naveen Krishna1 29-Oct-22 22:15pm    
Appreciate the help!
I'm not real familiar with Python but it looks like your going through all the scores and seeing if the letter is in the word. Since there are 2 'o's it will only get recorded one time.

Better to go through the word and look up the letter in score and add!
 
Share this answer
 
Comments
Naveen Krishna1 29-Oct-22 22:15pm    
Thanks for the help!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900