Created
February 16, 2022 09:10
-
-
Save mauro-balades/6f71399ba355cb627b31d84e2ee3cb3b to your computer and use it in GitHub Desktop.
Wordle for my IGCSE computer science course
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Import the random module | |
# for array shuffle | |
import random | |
# This is an array of available | |
# random words. | |
words = [ | |
"dog", "cat", "fish", "giraffe", | |
"moo", "spider", "lion", "apple", | |
"tree", "moon", "snake", "mountain lion", | |
"trooper", "burger", "nasa", "yes" | |
] | |
# Shuffle the items in the array | |
# and get the first element. | |
random.shuffle(words) | |
word = words[0] | |
# Have a bar with the | |
# length of the word to | |
# tell the user how many | |
# letters he guessed. | |
result = "-" * len(word) | |
# Define the number of | |
# "lives" left. | |
intents = 6 | |
# Iterate until the | |
# number of tries is | |
# 0. Because 0 == False. | |
while (intents): | |
# Print user's guesses | |
print("Current guesses: " + result) | |
# Get input from the user and save it in a variable. | |
# Note: | |
# We use f-string that let us allow use {} inside | |
# strings. | |
# | |
# example input: "Enter a guess (5 guesses left):" | |
guess = input(f"Enter a guess ({intents} guesses left): ") | |
# Set if the user guessed | |
# to False every time we | |
# iterate | |
guessed = False | |
# Iterate every character in the | |
# hidden word. Enumerate returns | |
# the index and the value. | |
for i, l in enumerate(word): | |
# Iterate every character | |
# of the user's guess. | |
for j, le in enumerate(guess): | |
# Check if user's guess | |
# is equal to the result's | |
# guess. We also check if | |
# the possitions are the same. | |
if le == l and i == j: | |
# Set that the user | |
# has a correct letter. | |
guessed = True | |
# Change a "-" to the correcter | |
# character at the same possition | |
result = result[:i] + l + result[i+1:] | |
# Stop the loop | |
break | |
elif le == l: | |
# Check if atleast that the guessed character | |
# exists in the word but it is not in the correct order. | |
print(f"Letter ({le}) exists but it is not in the right possition") | |
# If the user did not guess, | |
# we remove a "live" and | |
# print out "wrong letter!" | |
if not guessed: | |
print("Wrong letter!") | |
intents -= 1 | |
# Otherwise, if the guessed | |
# word is equal to the hidden | |
# word, the user has won. | |
if result == word: | |
print("You won!") | |
exit() | |
# If the number of "lives" is 0, | |
# we are going to asume that the | |
# user lost because he got more | |
# thatn 6 times a wrong character. | |
print("You lost!") |
overcommented lmao
ik, it was made for a person who just knows the really really basics of python.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
note: IGCSE does not require to use
enumerate
, there are other ways to compare 2 arrays.