suebriquet

Scrabble Scoring of All Words Greater than 3 Characters

Sep 1st, 2015
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.46 KB | None | 0 0
  1. # Print Scrabble Scoring of All Words Greater than 3 Characters
  2. # This script prints each word with its score like this:
  3. # word:    8
  4. # There is a tab between the : and the number
  5. # each word is printed on its own line, so you can easily import into an Excel sheet
  6. # You'll need a text file listing each word on its own line
  7. wordsFile = open("scrabble-words.txt", "r")
  8.  
  9. score = {"a": 1, "c": 3, "b": 3, "e": 1, "d": 2, "g": 2,
  10.          "f": 4, "i": 1, "h": 4, "k": 5, "j": 8, "m": 3,
  11.          "l": 1, "o": 1, "n": 1, "q": 10, "p": 3, "s": 1,
  12.          "r": 1, "u": 1, "t": 1, "w": 4, "v": 4, "y": 4,
  13.          "x": 8, "z": 10}
  14.          
  15. def scrabble_score(word):
  16.     word = word.lower()
  17.     wordArray = list(word)
  18.     wordLength = len(wordArray)
  19.     totalScore = 0
  20.    
  21.     for index in range(wordLength):
  22.         for key, value in score.items():
  23.             if wordArray[index] == key:
  24.                 totalScore = totalScore + value
  25.  
  26.     return word + ":\t"+str(totalScore)
  27.    
  28. def totalScoresList(wordsFile):
  29.     wordsList = [ line.rstrip('\n') for line in wordsFile.readlines()]
  30.     totalScoresList = ""
  31.     for word in wordsList:
  32.         word = str(word)
  33.         if len(word) > 3:
  34.             totalScoresList += str(scrabble_score(word)) + "\n"
  35.     return totalScoresList
  36.        
  37. # prints to a file called scrabble-scores.txt in this example
  38. scoresFile = open("scrabble-scores.txt", "w")
  39. scoresFile.write(totalScoresList(wordsFile))
  40. scoresFile.close()
Advertisement
Add Comment
Please, Sign In to add comment