Python @ DjangoSpin

PyPro #12 Computing Scrabble Score (using a Dictionary)

Buffer this pageShare on FacebookPrint this pageTweet about this on TwitterShare on Google+Share on LinkedInShare on StumbleUpon
Reading Time: 1 minutes

Computing Scrabble Score (using a Dictionary) in Python

Computing Scrabble Score (using a Dictionary) in Python

Python script to compute Scrabble score for a word using a dictionary.

## SCRABBLE SCORE
## Scrabble is a board game in which you get points for making words. Players get 7 tiles, which have letters printed on them, along with points. Each letter has a fixed number of points, letters used more often in the English language are given less points('S', 'T', vowels add 1 to the score), and letters used less often are given more points, based on how rarely they are used('Y' adds 4 points, 'X' adds 4 points, 'Q' adds 10). So, if you make a word 'say', then you get 1 + 1 + 4 i.e. 6 points. The player with the most points till the time the board is filled, wins. Following are the points associated with each letter: A: 1, B: 3, C: 3, D: 2, E: 1, F: 4, G: 2, H: 4, I: 1, J: 2, K: 5, L: 1, M: 3, N: 1, O: 1, P: 3, Q: 10, R: 1, S: 1, T:1, U: 2, V: 4, W: 4, X: 10, Y: 4, Z: 10
## THOUGHT PROCESS: Create a dictionary of letters(in uppercase or lowercase) along with their corresponding point values -> Prompt the user for a word -> convert the word to uppercase or lowercase, as per the case of letters in the dictionary -> Initialize the score variable to 0 -> Iterate over each character of the entered word, get the number of points it is worth and add to the score variable -> Output the score of a the enteredWord.

# Create a dictionary of letters along with their corresponding point values
points = {"A": 1, "B": 3, "C": 3, "D": 2, "E": 1, "F": 4, "G": 2, "H": 4, "I": 1, "J": 2, "K": 5, "L": 1, "M": 3, "N": 1, "O": 1, "P": 3, "Q": 10, "R": 1, "S": 1, "T": 1, "U": 1, "V": 4, "W": 4, "X": 8, "Y": 4, "Z": 10}

# Prompt the user for a word
word = input("Enter a word: ")

# Convert the word to uppercase and initialize the score variable to 0
word = word.upper()
score = 0

# Iterate over each character of the entered word, get the number of points it is worth and add to the score variable
for letter in word:
    score = score + points[letter]
    
# Print the accumulated score
print(word,"is worth", score, "points")
Try it here.

See also:

Buffer this pageShare on FacebookPrint this pageTweet about this on TwitterShare on Google+Share on LinkedInShare on StumbleUpon

Leave a Reply