Make Wordle in Python terminal in about 48 lines of code
Only need two things:
Get Visual Studio Code because why use any other IDEs Download VS Code
Get Python from Python website, Click Here!
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()
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()
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
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")
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
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()
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
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!!!")
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
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.