I am trying to create a Scrabble cheater code in python which can take in any letters and return every possible word, its score, and the total number of words possible. This includes up to two wildcards which can be any letter but have a value of zero.
I am able to do this for normal words, but if I include wildcards like '?' or '*' my code breaks (included in the code is how I expect the wild cards to be pulled but all the variables related to wild cards return empty or 0 values). I would like to stick to the way I wrote my code already but if there is a better way to look at it please let me know that as well.
scores = {"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 score_word(word):
total = 0
for letter in word:
if letter in "?" or "*":
total += 0
else:
total += scores[letter.lower()]
return total
def word_reader(rack):
with open("sowpods.txt","r") as infile:
raw_input = infile.readlines()
valid_words = [datum.strip('\n') for datum in raw_input]
if len(rack) in range(2,8):
wild_card_count = 0
scrabble_rack = []
scrabble_words = []
for i in rack:
if i == "*" or i == "?":
wild_card_count += 1
else:
scrabble_rack += [i.lower()]
if wild_card_count > 2:
print("Your rack contains too many wild cards.")
#find all the words that can be created from rack
for word in valid_words:
wild_card = ""
sr = [i for i in scrabble_rack]
valid = True
for letter in word:
if letter.lower() not in sr:
if wild_card_count > 0:
wild_card_count -= 1
wild_card += letter.lower()
continue
valid = False
break
sr.remove(letter.lower())
if not valid:
continue
else:
scrabble_words.append((ws.score_word(word) - ws.score_word(wild_card), word.upper()))
return scrabble_words, wild_card, wild_card_count, rack
else:
print("Your rack is not within the valid range of letters.")