Quantcast
Channel: Simplifying a Scrabble Word Finder - Python - Code Review Stack Exchange
Viewing all articles
Browse latest Browse all 8

Simplifying a Scrabble Word Finder - Python

$
0
0

I'm very new to coding and am trying to work through projects for practical review of what I know, but I would like to be able to simplify/improve what I've made.

This is a project I saw online that I tried to tackle today. The task was to create something that would provide a list of the best possible words to play given a set of letters on your rack.

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 calc_score(word):
    """Calculate the score of a given word."""
    word_score = 0
    for x in word:
        word_score += scores[x]
    return word_score

def look_up():
    """Create the variable containing the master list of words."""
    read_dict = open("sowpods.txt", "r")
    master = read_dict.read()
    read_dict.close()
    master = master.split("\n")
    return master

word_master = look_up()

def rack_check(f_rack):
    """Check the letters in the rack against the SOWPOD dictionary and
    append valid words to a list."""
    valid_list = []
    for item in word_master:
        letter_bank = list(f_rack)
        for letter in item:
            if letter in letter_bank:
                valid = True
                letter_bank.remove(letter)
            else:
                valid = False
                break
        if valid == True:
            valid_list.append(item)
    return valid_list

def dict_maker():
    """Create a dictionary of the words with their scores."""
    valid_dict = {}
    f_list = rack_check(list((raw_input("Letters: ")).lower()))
    for i in f_list:
        valid_dict[i] = calc_score(i)
    return valid_dict

def list_print():
    """Print a list of the words in descending order of value."""
    dict = dict_maker()
    sort_dict = sorted(dict.items(), key=lambda k: k[1], reverse=True)
    for x in sort_dict:
        print x[0], "-", x[1]

list_print()

Calling the "list_print()" function runs through all the other necessary functions, but I feel like it could be cleaner. Any feedback would be greatly appreciated.


Viewing all articles
Browse latest Browse all 8

Trending Articles