Download links for the .py and word list: wordle.py; word list



#STEP 1: Read a file that has a list of five-letter words. This is the "words.txt" file included in the assignment

#HElPFUL TIPS: The following code is used for reading a text file. Note that "lines" is a list that contains all words
#              from the file.

lines = []
with open('words.txt') as test:
    lines = test.readlines()


##STEP2: Randomly choose a word as your puzzle 

import random

words = []
for word in lines:
    words.append(word[:5])

puzzle_word = words[random.randint(0,len(words)-1)]


#STEP 3: Prompt the user to enter a five-letter word. Make sure that you check for the length of the input and print an error
#        message if it is not five letters, prompting them to enter again

# Print the meanings of the colors.
print("""Legend:
Black: Incorrect, no letters of the word.
Yellow: Letters of the word in but in the wrong spot.
Green: You win.""")

# Input for the word to guess.
guess_word = str(input("Enter a five-letter word: "))

# While-loop to ensure that the length of the word is exactly five.
while len(guess_word) != 5:
    if len(guess_word) < 5:
        guess_word = str(input("Too short. Enter a five-letter word!: "))
        continue
    elif len(guess_word) > 5:
        guess_word = str(input("Too long. Enter a five-letter word!: "))
        continue
    else:
        break
print(guess_word, "is your guess!")


#STEP 4: Check the input against your word and give feedback to the user
from colorama import Back, Fore, Style

# A counter for the amount of turns/chances.
chance_count = 0

# A list to save the correct letters of the attempted word
letter_save = []

# A list for the index number of the correct word
index_save = []

# While loop which will stop if the amount of chances equals six.
while chance_count < 6:

    # If-then for the  correct word totally guessed... it'll end the game.
    if guess_word.lower() == puzzle_word.lower():
        print(Back.GREEN + guess_word)
        print("You correctly guessed the word! Congrats & Game Over!")

        chance_count = 6
        break
    # Else, let's keep it going and have some fun with indexes and inputs!
    else:
        # Increment the chance counter each time.
        chance_count += 1
        
        # Clear the letter_save[] and index_save[] -- [] = lists
        letter_save.clear();index_save.clear()
        
        # For-loop which will scour for correct letters then append them
        # to lists accordingly.
        for i in range(0, len(puzzle_word)):
            for letter in puzzle_word[i]:
                if letter.find(guess_word[i]) == 0:
                    letter_save.append(letter.lower())
                    index_save.append(i)
        
        # These if conditionals are for the amounts of letters entered for input.
        if len(letter_save) == 5:
            continue
        if len(letter_save) == 0:
            print(Back.BLACK + Fore.WHITE + guess_word)
            print(Style.RESET_ALL)
        if len(letter_save) > 0:
            print(Back.YELLOW + "You got a few correct!")            
            print(guess_word)
            print(Back.YELLOW)
            print(letter_save)
            letter_save.clear()
            print(Style.RESET_ALL)
    
    # Prompt to enter another choice of word.
    guess_word = str(input("Incorrect! Make another presumption! Be bold! "))
    
    # While-loop for input validation-- if the letters entered equal five.
    while len(guess_word) != 5:
        if len(guess_word) < 5:
            guess_word = str(input("Too short. Enter a five-letter word!: "))
            if chance_count == 6:
                break
            else:
                continue
        elif len(guess_word) > 5:
            guess_word = str(input("Too long. Enter a five-letter word!: "))
            if chance_count == 6:
                break
            else:
                continue
        else:
            continue