Skip to content

Instantly share code, notes, and snippets.

@mjdargen
Last active October 19, 2022 22:03
Show Gist options
  • Save mjdargen/4fcafb287aaafc474cfdcc1ba114adf0 to your computer and use it in GitHub Desktop.
Save mjdargen/4fcafb287aaafc474cfdcc1ba114adf0 to your computer and use it in GitHub Desktop.
from random import choice
import os
with open("./bilingual/anagrams.txt", "r") as f:
words = f.read().splitlines()
word = choice(words)
while len(word) < 4 or len(word) > 6:
word = choice(words)
words.remove(word)
i = 0
while (i < len(words)):
check = word
flag = True
for letter in words[i]:
if letter in check:
check = check.replace(letter, '', 1)
else:
flag = False
if not flag:
words.pop(i)
else:
i += 1
anagrams = words.copy()
guessed = ['_ ' * len(anagrams[i]) for i in range(len(anagrams))]
# guessed = []
# for every possible anagram
# for w in anagrams:
# new_word = "" # clear out word
# for i in range(0, len(w)):
# new_word += "_ " # add "_ " for ever letter
# guessed.append(new_word)
print(guessed)
# Now guess them
done = False
correct = 0
while not done:
print(f"The word is \'{word}\'.")
print(f"There are {len(anagrams)-correct} anagrams remaining.")
# print 6 anagrams per line
i = 0
while i < len(anagrams) // 6 + 1:
# 3 spaces between each word so it's legible
print(" ".join(guessed[i*6:(i+1)*6]))
i += 1
print()
guess = input("Enter an anagram: ")
os.system("clear")
# if the user types "I quit", end the game
if guess == "I quit":
break
# if the guess in the valid anagrams list, update it
if guess in anagrams:
index = anagrams.index(guess)
guessed[index] = guess
correct += 1
# else incorrect guess
else:
print("Nah, you guessed wrong...")
# check to see if there are any anagrams remaining.
remaining = False
# check to see if any word in guessed has undescores
for w in guessed:
if "_" in w:
remaining = True
# if there were no underscores, we are done!
if remaining == False:
done = True
# check to see if they either won or quit
print(anagrams)
print(guessed)
# if they match, you won
if anagrams == guessed:
print("You won!")
# else you quit
else:
print("Nice try!")
print(f"You got {correct} correct out of {len(anagrams)}.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment