from random_words import RandomWords
from colorama import Fore, Back, Style
from random_words import RandomWords
from colorama import Fore, Back, Style
Assuming that someone wants to run your program, how would he run it without having this random_wordsrandom_words thing? You should include it with the rest of your code unless it's an official Python module.
I suggest you check pep0008 https://www.python.org/dev/peps/pep-0008/PEP0008, the official Python style guide.
#define function to clear the screen
def clear_screen():
return os.system('cls')
#define function to clear the screen
def clear_screen():
return os.system('cls')
Docstrings:
Python documentation strings (or docstrings) provide a convenient way of associating documentation with Python modules, functions, classes, and methods. An object's docstring is defined by including a string constant as the first statement in the object's definition. You might write a docstring instead of the comment above the function.
def clear_screen():
"""Clear the screen."""
return os.system('cls')
Comments: You might want to include comments on a need basis and ommitomit the unecessary explanationsunnecessary explanations; according to PEP0008 you should use comments sparingly. lots of things are self-explanatory.
And a comment starts with # comment# comment not #comment#comment.
# import packages
# define function to clear the screen
# opening statement
Blank lines: (pep008) Extra blank lines may be used #(sparingly) to separate groups of related functions. Blank lines may be omitted between a bunch of related one-liners (e.g. a set of dummy implementations).
Too long lines: PEP 8 suggests lines should be limited to 79 characters.
-
while game_on is True:
can be expressed:
while game_on:
Missing white space around operators: 1 space on both sides of binary operators(+ - / * // & | ^ % = > < == !=) except for function default values.
hidden_word = ('_')*length_of_word_to_guess
print (f'You correctly guessed the word, which is ' + Fore.GREEN +
f'"{word_to_guess}"'+ Style.RESET_ALL+'.')
Invalid input: no catching of problematic inputs such as numbers (7 in the case above, the program indicates that I already tried 7 before).
sameThe same goes for invalid inputs in the first question (do you want to start the game? y/n) - suppose a user entered yes or no instead of n or y or N or Y, then the program should indicate the invalid input or have cases covering such possible occurrences (it's not very unlikely that someone enters yes or no).
Clear screen function os.system('cls')os.system('cls') clears only windows systemon Windows systems, os.system('clear')os.system('clear') for unixUnix (mac-linuxincluding Mac and Linux) systems otherwise are going to throw some error,error; you should indicate that in the docstring or implement another function to support unixUnix systems (I have a macbook, so I had to change it to run properly).
Would you like to start a new game? infinite loop if the answer is no (n) at the very start of the game, so the user is compelled to play,play; he has no actual choice!
os.system('cls')
print ('Welcome to the Hangman!\n')
os.system('cls')
print ('Welcome to the Hangman!\n')
whyWhy use os.system('cls')os.system('cls') and you already defined a function to do so? why not ...
#Functions
Python Functions. AA function is a block of code which only runs when it is called. You can pass data, known as parameters, into a function. A function can return data as a result. You can enclose your code into separate functions that perform differents tasks,different tasks; it's better for readability/modularity and easier to debug/ change (what if you want to change some value that keeps repeating through the code?).
Here's a refactored version of the code:
You
You will need to download this text file containing lots of wordsword list for running the code (put the file in the same folder with the script)
https://drive.google.com/file/d/1am_sdLI-NCs31G-O7b-7pJKx9t5QIGqs/view?usp=sharing.