Sorry, your browser is not supported
To have the best experience using Skillshare, we recommend that you use one of these supported browsers.



Scrabble Cheater

f = open("sowpods.txt","r") # Open and read the file sowpods.txt
words = []
for word in f:
    word = word.strip()
    words.append(word) #Adds all the words from sowpods.txt to a list words
number_of_words = len(words)
value_of_letters = {"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} #Creates a dictionary with the value of each letter
import sys
rack = sys.argv[1] #The letters on the rack will be entered in the command window
valid_words = []
for word in words:
    candidate = True #Assumes that the word will be valid
    rack_letters = list(rack) #Turns the values entered in rack into a list
    for letter in word:
        if letter not in rack_letters:
            candidate = False    #If a letter in a word is not in rack_letter it is not a valid word
            rack_letters.remove(letter) #When a letter is in rack_letter, the letter should be removed, so it is not reused
    if candidate == True:
        total = 0
        for letter in word:
            total = total + value_of_letters[letter]
        valid_words.append([total,word]) #Adds all valid words to a list with their word value
valid_words.sort() #Sorts the valid words based on their word value
for entry in valid_words:
    score = entry[0]
    word = entry[1]
    print(str(score) + " " + word) #Prints the value of the word, followed by the word


Please sign in or sign up to comment.