r/learnpython • u/Early_Ad_5386 • 44m ago
doing beginner project of wordle type game as beginner. Any tips?
Any tips? and what projects should I try after this to learn more about python.
Learned about ANSI color and more about list doing it. It was fun. Any improvements?
import random
from words_list import WORDS
# Importing the list of words from worlde_list.py
# ANSI color codes
GREEN = "\033[42m\033[30m"
# Green background, black text
YELLOW = "\033[43m\033[30m"
# Yellow background, black text
GRAY = "\033[100m\033[37m"
# Gray background, white text
RESET = "\033[0m"
def play():
random_word = random.choice(WORDS)
# Randomly selects a word from the list
attempts = 6
# Incorrect attempts remaining
print("Welcome to Worlde! You have 6 attempts to guess the word.")
while attempts > 0:
print(f"You have {attempts} attempts remaining.")
guess = input("Enter a word: ").upper()
# Player inputs a guess, converted to uppercase
if not guess.isalpha():
print("Please enter alphabets. Numbers and symbols aren't allowed.")
continue
elif len(guess) != len(random_word):
print(f"Please enter a word which has {len(random_word)} letters.")
continue
letter_count = {}
for letter in random_word:
if letter in letter_count:
letter_count[letter] += 1
else:
letter_count[letter] = 1
# Create an empty list to store the feedback
feedback = [""] * len(guess)
#Check for green letters
for i in range(len(guess)):
if guess[i] == random_word[i]:
feedback[i] = GREEN + guess[i] + RESET
letter_count[guess[i]] -= 1
continue
#For both yellow and green letters, we need to check the remaining letters in the random word
for i in range(len(guess)):
if guess[i] == random_word[i]:
continue
# Skip already matched letters
elif guess[i] in letter_count and letter_count[guess[i]] > 0:
## Checks if the letter exists in the word and has any unused occurrences remaining.
feedback[i] = YELLOW + guess[i] + RESET
letter_count[guess[i]] -= 1
else:
feedback[i] = GRAY + guess[i] + RESET
attempts -= 1
print("".join(feedback))
# Displays the feedback for the guessed word
if guess == random_word:
print("Congratulations! You've guessed the word correctly.")
break
if attempts == 0 and guess != random_word:
print(f"Sorry, you've run out of attempts. The correct word was: {random_word}")
play()