Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Print Scrabble Scoring of All Words Greater than 3 Characters
- # This script prints each word with its score like this:
- # word: 8
- # There is a tab between the : and the number
- # each word is printed on its own line, so you can easily import into an Excel sheet
- # You'll need a text file listing each word on its own line
- wordsFile = open("scrabble-words.txt", "r")
- 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()
- wordArray = list(word)
- wordLength = len(wordArray)
- totalScore = 0
- for index in range(wordLength):
- for key, value in score.items():
- if wordArray[index] == key:
- totalScore = totalScore + value
- return word + ":\t"+str(totalScore)
- def totalScoresList(wordsFile):
- wordsList = [ line.rstrip('\n') for line in wordsFile.readlines()]
- totalScoresList = ""
- for word in wordsList:
- word = str(word)
- if len(word) > 3:
- totalScoresList += str(scrabble_score(word)) + "\n"
- return totalScoresList
- # prints to a file called scrabble-scores.txt in this example
- scoresFile = open("scrabble-scores.txt", "w")
- scoresFile.write(totalScoresList(wordsFile))
- scoresFile.close()
Advertisement
Add Comment
Please, Sign In to add comment