Skip to content

Instantly share code, notes, and snippets.

Last active Dec 13, 2021
What would you like to do?
Simple hangman game guesser written in Python.
# Simple hangman guesser by @KTibow
# Get the possible words from the file
import string
all_words = open("words_alpha.txt").read().splitlines()
# Get how long the word is
word_length = int(input("How long is the word? "))
word_letters = [[] for i in range(word_length)]
possible_words = []
def find_representation(not_valid):
if len(not_valid) == 25:
right_letter = list(string.ascii_lowercase)
[right_letter.remove(i) for i in not_valid]
return right_letter[0]
return "_"
while True:
letter_probabilities = {}
possible_words = []
for word in all_words:
# Check if the word is the right length
if len(word) != word_length:
# Check if the word has the right letters
word_is_valid = True
for i, not_options in enumerate(word_letters):
if word[i] in not_options:
word_is_valid = False
if not word_is_valid:
# Add the word to the list of possible words
# If the word is valid, add it to the letter_probabilities
for i in range(word_length):
if len(word_letters[i]) < 25:
if word[i] in letter_probabilities:
letter_probabilities[word[i]] += 1
letter_probabilities[word[i]] = 1
# Find the most likely letter and check it
opportunity_count = len(possible_words)
if opportunity_count <= 1:
print("Found the word, it is: " + possible_words[0])
letter_probabilities = {
k: v
/ opportunity_count
* v # This amount of the time, the letter is used, and this many would remain after that
+ (1 - v / opportunity_count)
* (
opportunity_count - v
) # This amount of the time, the letter is not used, and this many would remain after that
for k, v in letter_probabilities.items()
best_letter = min(letter_probabilities, key=letter_probabilities.get)
f"Best letter to guess: {best_letter} ({letter_probabilities[best_letter] / len(possible_words) * 100:.2f}% chance of being in a word)"
f"{len(possible_words)} possible words ({format(len(possible_words) / len(all_words) * 100, '.3f')}% out of total)"
if len(possible_words) <= 20:
print("Possible words:", possible_words)
for i in range(word_length):
if len(word_letters[i]) == 25:
representation = [find_representation(j) for j in word_letters]
representation[i] = "?"
user_response = input(
f"Is the question mark in {''.join(representation)} the letter {best_letter} (y/ )? "
if user_response == "y":
not_options = list(string.ascii_lowercase)
word_letters[i] = not_options
print("".join([find_representation(i) for i in word_letters]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment