Wordle in Python (Part-1)
Wordle is a popular online word-guessing game that challenges players to guess a randomly generated five-letter word. The game is simple yet addictive, and has gained a massive following online.
In Wordle, the player is given a secret five-letter word, and must guess the word by inputting five-letter guesses. After each guess, the game will indicate which letters are correct and in the correct position, and which letters are correct but in the wrong position. The player has six chances to guess the word correctly, and must use strategy and logic to narrow down the possibilities.
One of the great things about Wordle is its accessibility. The game is easy to learn, and can be played by anyone, regardless of age or skill level. Additionally, the game can be played on a wide range of devices, from desktop computers to mobile phones and tablets, making it easy to play on the go or during downtime.
Today we will code up a very simplified version of the wordle game in python. In next article we will try to add more features to the game.
Get M Haque’s stories in your inbox
Join Medium for free to get updates from this writer.
Here’s a Python code for a simplified version of the Wordle game, where the player needs to guess a secret 5-letter word. This code uses a dictionary of 100 random words and allows the player to choose the number of rounds (best of 3, 5, or 10).
import random
# Define a list of 100 random 5-letter words
word_list = ['apple', 'banana', 'cherry', 'daisy', 'eagle', 'fable', 'grape', 'hazel', 'ivory', 'jolly',
'kitty', 'laser', 'mango', 'naval', 'olive', 'piano', 'quack', 'razor', 'shark', 'tiger',
'umbra', 'vapor', 'waltz', 'xylophone', 'yacht', 'zebra']
# Define the number of rounds (best of 3, 5, or 10)
rounds = int(input("How many rounds do you want to play? (3, 5, or 10) "))
# Initialize the player's score
player_score = 0
# Play the game for the specified number of rounds
for i in range(rounds):
# Select a random word from the word list
secret_word = random.choice(word_list)
# Initialize the number of guesses and the list of correct guesses
guesses = 0
correct_guesses = []
# Keep guessing until the player has guessed the word or has run out of guesses
while guesses < 5:
# Prompt the player to guess a letter
guess = input("Guess a letter: ")
# Check if the guessed letter is in the secret word
if guess in secret_word:
# Add the guessed letter to the list of correct guesses
correct_guesses.append(guess)
# Check if the player has guessed all the letters in the word
if set(correct_guesses) == set(secret_word):
# Increment the player's score and break out of the loop
player_score += 1
print("Congratulations! You guessed the word in", guesses + 1, "guesses.")
break
# Increment the number of guesses
guesses += 1
# Print the secret word at the end of the round
print("The word was:", secret_word)
# Print the final score
print("Final score:", player_score)Input:
How many rounds do you want to play? (3, 5, or 10) 5Output:
Guess a letter: a
Guess a letter: e
Guess a letter: i
Guess a letter: o
Congratulations! You guessed the word in 4 guesses.
The word was: olive
Guess a letter: a
Guess a letter: e
Guess a letter: i
Guess a letter: o
Guess a letter: u
The word was: razor
Guess a letter: a
Guess a letter: e
Guess a letter: i
Guess a letter: o
Guess a letter: u
The word was: tiger
Guess a letter: a
Guess a letter: e
Guess a letter: i
Congratulations! You guessed the word in 3 guesses.
The word was: navya
Guess a letter: a
Guess a letter: e
Guess a letter: i
Guess a letter: o
Guess a letter: u
The word was: waltz
Final score: 2One great feature of Wordle is its replayability. Each time the game is played, a new, randomly generated word is selected, ensuring that the game remains fresh and challenging each time. The game also has an element of competition, as players can try to beat their own high score or compete with friends to see who can guess the word the fastest.
Overall, Wordle is a fun and engaging game that can provide hours of entertainment. Its simplicity, accessibility, and replayability make it a favorite among players of all ages and skill levels. Whether you’re looking to pass the time, challenge your mind, or simply have some fun, Wordle is definitely worth a try.
Keep lookout for the 2nd article covering more features/code for the Wordle game.




