Make Wordle in Python terminal in about 48 lines of code

Table of Content

  1. Setup Enviorment
  2. Setting the template and Intro function
  3. Input and word logic cases
  4. Word guess logic
  5. Use a text file instead of a word list

Image of Final Project, Wordle in Terminal

Finish Product

Only need two things:

Get Visual Studio Code because why use any other IDEs Download VS Code

Get Python from Python website, Click Here!

What is Wordle and what does it need?

Wordle is a word guessing game, that the user guess a random word and get clues based on past guess

So some things we will need, randomness, and a word bank and game logic.

import random

words = ['gamed', 'usfgdc', 'tired', 'cofee', 'ineed']

def main():

main()

Probably a good idea to include some Instructions to the game at the top, so lets add a function

def Intro():
    print("This is Wordle")
    print("\t✅ - letter is the word and is in correct spot")
    print("\t🟨 - letter is the word and is not in the right spot")
    print("\t _ - letter is not in the word")
    print()

Call the function in your main

import random

words = ['gamed', 'usfgdc', 'tired', 'cofee', 'ineed']

def Intro():
    ...

def main():
    Intro()
    
main()

Picking a random word in the word bank

def main():
    Intro()
    hiddenWord = words[random.randint(0, len(words)-1)]

Also wordle has a limited number of trys, so lets add that too.

hiddenWord = words[random.randint(0, len(words)-1)]
attempts = 5    # NEW CODE HERE

Duration 0:07:00

Design and Logic

What we want:

def main():
    Intro()
    hiddenWord = words[random.randint(0, len(words)-1)]
    attempts = 5

    # NEW CODE HERE
    while (attempts > 0):
        guess = input("Enter Guess (5 Letter Word): ").lower()

        if (guess == hiddenWord):
            print("Good Guess!!!")
            break
        else:
            # Do wordle clue logic
        
        attempts -= 1
        print(f"{attempts} are left.\n")

Design and Logic

Doing it in code

if (guess == hiddenWord):
    print("Good Guess!!!")
    break
else:
    # NEW CODE HERE
    output = ""
    for hiddenLetter, guessLetter in zip(hiddenWord, guess):
        if (hiddenLetter == guessLetter.lower()):
            output += " ✅ "

Since if statements will skip else-if and else statements when true, we can just stack these statements logically for easy implementation

else:
    output = ""
    for hiddenLetter, guessLetter in zip(hiddenWord, guess):
        if (hiddenLetter == guessLetter.lower()):
            output += " ✅ "

        # NEW CODE HERE
        elif (guessLetter in hiddenWord):
            output += " 🟨 "
        else:
            output += " _ "

    print(output)       #outside of the loop

Lets reveal the secret word if user run out of attempts

            attempts -= 1
            print(f"{attempts} are left.\n")

# NEW CODE HERE
            if (attempts == 0): 
                print(f"All attempts were used, the word was {hiddenWord.upper()}.")
# NEW CODE END

main()

Now Your wordle is already functional logically, HOORAY

However, you may notice that you can enter non-real words and it still runs unlike wordle

Additionally, you also may realize that if we want more words, we will have to expand our list which makes it ugly and wonder why do that manually

Design and Logic

Coding our Design

while (attempts > 0):
    guess = input("Enter Guess (5 Letter Word): ").lower()

    # NEW CODE HERE
    if (guess not in words):            # because we are arrogant if we dont know it, it doesn't exist
        print("Word not in word list, Try another word\n")
        continue
    # NEW CODE END

    if (guess == hiddenWord):
            print("Good Guess!!!")

Opening an external file in python

import random

# NEW CODE HERE
# wordslist.txt was taken from here https://gist.github.com/dracos/dd0668f281e685bad51479e5acaadb93
with open('wordslist.txt', 'r') as wordsFile:
    words = wordsFile.read().split('\n')

# words = ['gamed', 'usfgdc', 'tired', 'cofee', 'ineed']        Remove this if you want

Optional, Rather than having Colored squares you can have colored text by replaceing the logic part with this code

if (hiddenLetter == guessLetter.lower()):
    output += "\033[32m " + guessLetter.upper()
elif (guessLetter in hiddenWord):
    output += "\033[33m " + guessLetter.upper()
else:
    output += "\033[37m"+ " _ "

output += "\033[37m"

One more thing to add to your resume, proving your Python experience. Please check-in College of engineering pays us depending on sign-in numbers

Fun Fact, the seal you seal you see here is called solly, solly is quite thin for a seal. Its because the College of Engineering did give solly money for his meal, if you want to make a difference its as simple as scanning the QR code.

Soly Image Caption