I see if var is number in multiple places. Specifically, lines 29, 96, 109. One could argue that is reads better than ==, but the two are computationally different. (Actually, is breaks for large numbers.) Use == instead. See also: Is there a difference between “==” and “is”?
Functional Programming and ImmutabilityThinking Functionally
And this reads "substitute is '.' if this else that". (Note how the statement is now declarative and the variable becomes the subject of the statement.)
- While-loops tend to be imperative construct (telltelling the interpreter to loop while something isn't true).
- Since this is a dynamic game and since it's a single while-loop, you'll inevitably modify the state of surrounding variables to either keep track of progress, whether the game is finished or not, etc.
Lipovača: "But you just said## Line 81 ## # the number of guesses the computer has made. num_of_guesses = 0 ## Line 127 ## # add our guess to the total listing of guesses. num_of_guesses += 1num_of_guessesis 0!"
Solution? Recursion. Pass in the mutable variables as arguments to the function and recurse all the way to the end. (Or of course, you could stick with the more readable while-loop. Some things are inevitable – sigh.)
This is really helpful in the world of FP. What does a function receive? What does it return? This allows you to reason with the input and output of functions. See also: What are Type hints in Python 3.5
Yes, there are too manyquite a lot. AndSome are unnecessary... and some of them are outright lies redundantuntruths.
Lines 20-21: What if the implementation of get_words changes? This comment becomes obsolete.
What if the implementation of get_words changes? This comment becomes obsolete.
Lines 47-48: No printing here.
No printing here. You print it sometime later in the game loop, but not here. Here, there's only a return, which in itself doesn't do any printing.
Instead, commentconsider commenting what each function does, preferably using Python doc-strings.
def get_words(word_len):
"""
Returns a list of words each with length equal to `word_len`.
"""
We don't needAs above, you can choose to bother withleave out the details of the implementation since opening. Sure, readingget_words will open, read, and closingclose a file, but this won't have any side effectsside-effects1. Perhaps in the future, you might want to load the words from a database, and the docstringdoc-string won't need to be updated, because the input and output are unchanged.
1 – Unless if say, you're in a multi-threaded environment where the files will be accessed from different threads concurrently.
We also don't need the comment on line 20: # Load the words in from the words.txt file.. We can simply scroll to get_words and read the docstringdoc-string to know what it does.
See also: Self-Documenting Code; What is self-documenting code and can it replace well-documented code?
I'm seeing quite a lot of comprehensions and few forno for-loop blocks. This is good. Usinga merit: using a for-loop block with colon and suite bears the air of imperative programming (telltells the interpreter to loop over thisan iterable), but comprehensions are more functional as they pack your values into a handy list/set/dictionary/generator expression.