import random
import os
def read_data(filepath="./data.txt"):
words = []
with open(filepath, "r", encoding="utf-8") as f:
for line in f:
words.append(line.strip().upper())
return words
def run():
data = read_data(filepath="./data.txt")
chosen_word = random.choice(data)
chosen_word_list = [letter for letter in chosen_word]
chosen_word_list_underscores = ["_"] * len(chosen_word_list)
letter_index_dict = {}
for idx, letter in enumerate(chosen_word):
if not letter_index_dict.get(letter):
letter_index_dict[letter] = []
letter_index_dict[letter].append(idx)
while True:
os.system("clear")
print("Find the hidden word!")
for element in chosen_word_list_underscores:
print(element + " ", end="")
print("\n")
letter = input("Input a letter: ").strip().upper()
assert letter.isalpha(), "You can only input letters"
if letter in chosen_word_list:
for idx in letter_index_dict[letter]:
chosen_word_list_underscores[idx] = letter
if "_" not in chosen_word_list_underscores:
os.system("clear")
print("Congratulations the word is...", chosen_word)
break
if __name__ == '__main__':
run()