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

Answer by Lukasz Salitra for Simplifying a Scrabble Word Finder - Python

$
0
0

The program is pretty good and clean so far, the docstrings are clear and it's easy to follow what's going on.

The scores dictionary is a constant, so it should be all uppercase.


Use a context manager to open the text file. This way you don't have to worry about the file not being closed, as the context manager does it for you.

def look_up():
    """Create the variable containing the master list of words."""
    with open('sowpods.txt', 'r') as f:
        return f.read().split('\n')

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

This can be rewritten using a list comprehension.

def calculate_score(word):
    """Calculate the score of a given word."""    
    return sum([SCORES[letter] for letter in word])

As you can see, we don't need to initialize an empty variable. Python is a self-documenting language.x isn't really a descriptive name, so I took the liberty to change it to letter. This one is a nitpick really, but I also change the function name to calculate_score.


Staying on comprehensions, you can use a dictionary comprehension in dict_maker. Here, we create a list of letters using list comprehension and then return a dictionary containing the word and its score.

def dict_maker():
    """Create a dictionary of the words with their scores."""
    letters = [letter for letter in raw_input('Letters: ').lower()]
    return {word: calculate_score(word) for word in rack_check(letters)}

I'm a little short on time to look over the logic, so I will finish the rest later, but here's some quick observations.

  • In list_print you assigned a dictionary using dict as its name. This is fairly uncommon practice, as it shadows a builtin. More on builtins.
  • word_master = look_up() should be contained in rank_check function. Furthermore, you can even replace for item in word_master: with for item in look_up()

Viewing all articles
Browse latest Browse all 8

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>