Home > other >  Storing colored strings in a list
Storing colored strings in a list

Time:07-29

I just needed some help regarding termcolor, as I am trying to make wordle, but when it came to color, I was trying to append a colored string to a list, but didn't appear to work, and just gave back a bunch of random numbers a letters.

Here is the code where the issue lies:

def letter_analyzation(guess, word, x):
    guess_list.clear()
    for i in range(0, 5, 1): # Making a loop to shorten code
        if guess[i] in word:
            if guess[i] != word[i]:
                x = colored(str(word[i]), 'yellow')
                guess_list.append(x)
            if guess[i] == word[i]:
                x = colored(str(word[i]), 'green')
                guess_list.append(x)
            else:
                x = colored(str(word[i]), 'grey')
                guess_list.append(x)
    return guess_list

Here is the rest of the code if you need it:

import random
from termcolor import colored

print('Wordle')

# Giving values to some variables:
word_list = ['apple', 'fudge', 'caked', 'brain', 'lives', 'stomp', 'epoxy', 'blood', 'board', 'broad', 'baste', 'grate', 'spade', 'slice', 'price', 'curse', 'based', 'brace', 'place', 'creed', 'greed']
previous_guesses = []
guess_list = []
x = ''

def word_picker(): # Generates a random number as the index for the word list.
    random_num = random.randint(0, len(word_list) - 1)
    word = word_list[random_num]
    return word

def input_word(): # Using recursion to take a guess
    guess = input('Guess: ')
    if len(guess) == 5:
        return guess # Makes all values of the guess to be lowercase to make my life easier.
    else:
        input_word()

def letter_analyzation(guess, word, x):
    guess_list.clear()
    for i in range(0, 5, 1): # Making a loop to shorten code
        if guess[i] in word:
            if guess[i] != word[i]:
                x = colored(str(word[i]), 'yellow')
                guess_list.append(x)
            if guess[i] == word[i]:
                x = colored(str(word[i]), 'green')
                guess_list.append(x)
            else:
                x = colored(str(word[i]), 'grey')
                guess_list.append(x)
    return guess_list

def check_win(guess, word): # Looks for a win 
    if guess == word:
        win = True
        return win
    else:
        win = False
        return win

def Wordle():
    # Making global variables to avoid any errors:
    global word_list
    global previous_guesses
    global guess_list

    # Defining different variables so a restart case is possible:
    win = False
    counter = 0
    previous_guesses.clear()
    
    word = word_picker() # Picking a random word

    while counter <= 5: # A while loop to make sure that the player doesn't exceed 5 turns
        counter  = 1
        guess = input_word()
        guess_list = letter_analyzation(guess, word, x)
        print(guess_list)
        win = check_win(guess, word)
        if win == True:
            print('Congrats, you have won!')
            break

        print('Previous guesses:')
        previous_guesses.append(guess)
        print(previous_guesses)

    if counter > 5:
        print('You have lost')

Wordle()

reset = input('Would you like to try again? y/n: ')
if reset == 'y':
    Wordle()

As always, thank you so much for your time, and I look forward to reading all of your comments.

CodePudding user response:

I tried out your code. Basically, you are getting that quirky looking stuff because you are actually populating a list instead of creating a string with your variable "guess_list". Revising your code and treating that variable as a list, here is a tweaked version of your code.

import random
from termcolor import colored

print('Wordle')

# Giving values to some variables:
word_list = ['apple', 'fudge', 'caked', 'brain', 'lives', 'stomp', 'epoxy', 'blood', 'board', 'broad', 'baste', 'grate', 'spade', 'slice', 'price', 'curse', 'based', 'brace', 'place', 'creed', 'greed']
previous_guesses = []
guess_list = []
x = ''

def word_picker(): # Generates a random number as the index for the word list.
    random_num = random.randint(0, len(word_list) - 1)
    word = word_list[random_num]
    return word

def input_word(): # Using recursion to take a guess
    guess = input('Guess: ')
    if len(guess) == 5:
        return guess # Makes all values of the guess to be lowercase to make my life easier.
    else:
        input_word()

def letter_analyzation(guess, word, x):
    guess_list = ""          # Treat this as a string and not a list
    for i in range(0, 5, 1): # Making a loop to shorten code
        if guess[i] in word:
            if guess[i] != word[i]:
                x = colored(str(guess[i]), 'yellow')
                guess_list  = x
            if guess[i] == word[i]:
                x = colored(str(guess[i]), 'green')
                guess_list  = x
        else:                   # Moved the indentation of this bit.
            x = colored(str(guess[i]), 'red')
            guess_list  = x
    return guess_list

def check_win(guess, word): # Looks for a win 
    if guess == word:
        win = True
        return win
    else:
        win = False
        return win

def Wordle():
    # Making global variables to avoid any errors:
    global word_list
    global previous_guesses
    global guess_list

    # Defining different variables so a restart case is possible:
    win = False
    counter = 0
    previous_guesses.clear()
    
    word = word_picker() # Picking a random word

    while counter <= 5: # A while loop to make sure that the player doesn't exceed 5 turns
        counter  = 1
        guess = input_word()
        guess_list = letter_analyzation(guess, word, x)
        print(guess_list)
        win = check_win(guess, word)
        if win == True:
            print('Congrats, you have won!')
            break

        print('Previous guesses:')
        previous_guesses.append(guess)
        print(previous_guesses)

    if counter > 5:
        print('You have lost')

Wordle()

reset = input('Would you like to try again? y/n: ')
if reset == 'y':
    Wordle()

I also repositioned the "else" statement in your analyzation function so that it works in concert with the "if guess[i] in word" test. Following was a test run (I snuck a peek at the words). FYI, I changed the one color to red so that I could see it on my terminal.

Sample

I hope that helps.

Regards.

  • Related