python hangman solver – Code Review Stack Exchange

Welcome to Code Review!


In python, it is common (and recommended) to follow the PEP-8 style guide for writing clean, maintainable and consistent code.

Functions and variables should be named in a lower_snake_case, classes as UpperCamelCase, and constants as UPPER_SNAKE_CASE.

Type hinting

With newer python versions, you can make use of type hinting to give a brief overview of the type of variables and function parameters.

Counting letters

Python provides an inbuilt collections.Counter for you to make use of.


The comments don’t really help here. Also, prefer writing docstring over comments for functions.

Variable names

You open a file, and name its pointer as words, whereas the function is returning a list of words; called reader, which in turn is stored in a variable called words?

Weird conversions

You generate a mapping of letters to their respective counts, as a dictionary (hash-map/table/lookup), then convert it to list of 2-valued tuple, sort that, and convert back to dictionary. Why? dict has $ O(1) $ lookup and sorting makes no sense in your case.

Iteration with index

You use the following loop structure:

for i in range(len(words)):

where, i in itself is serving no purpose. Every occurrence of usage of i is of the form words(i). You can just iterate over the values of words list itself:

for word in words: