Python comparison problem for wordle type game

Question:

I am making a wordle game in python.
For that i need to compare the wordle word(mentioned as random_word in the code) to the input by user.

The problem is i am unable to devise a method to handle repititions of letters in any word.

Here is my compare function:

def comparison_func(user_input,random_word):
    random_word_list = []
    output = ["","","","",""]
    for i in range(len(random_word)):
        random_word_list.insert(i,random_word[i].lower())
        
    #check if input letters are in the list
    for x in range(len(user_input)):

        if user_input[x] in random_word_list:
            if user_input[x] == random_word[x]:
                output[x] = user_input[x].upper()
            else:
                output[x] = user_input[x].lower()
        else:
            output[x] = "_"


    output_string = ""
    for element in output:
        output_string += element

    return output_string 

In the output a Capital letter represents that it is in the right place.
The lowercase letter represent it is in the word but at incorrect place right now.
And the underscore represent that the letter is not in the word.

Asked By: Igneous Chunk

||

Answers:

I’d suggest you remove from random_word_list at each letter, so you won’t tell there is 2 misplaced letters when there is only one in the word to guess

  • don’t need to set the output one by one in a predefined list, just append to a string
  • remove every letter that you find in random_word
def comparison_func(user_input, random_word):
    assert len(user_input) == len(random_word), "Sizes differ"
    
    letters = list(random_word.lower())
    result = ""

    for l, l_guess in zip(user_input, random_word):
        if l in letters:
            result += l.upper() if l == l_guess else l.lower()
            letters.remove(l)
        else:
            result += "_"

    return result
Answered By: azro

The word length needs to be compared to give the user a fair chance at guessing. This could be done a lot of ways but I chose this so the user can know how long the word they’re guessing is. Also, both words need to be upper case or both lower case so the code properly compares the letters.

def comparison_func(user_input,random_word):
    user_input = user_input.lower()
    random_word = random_word.lower()
    if len(user_input) < len(random_word):
        print("User's answer is {} letter(s) shorter than today's word".format(len(random_word) - len(user_input)))
    elif len(user_input) > len(random_word):
        print("User's answer is {} letter(s) longer than today's word".format(len(random_word) - len(user_input)))
    else:
        output = ""
        for x in range(len(user_input)):
            if user_input[x] in random_word:
                if user_input[x] == random_word[x]:
                    output = output + user_input[x].upper()
                else:
                    output = output + user_input[x].lower()
            else:
                output = output + "_"
        output_string = ""
        for element in output:
            output_string += element

        return output_string 
Answered By: Jackson Rini

You could do this in two steps using zip():

  1. identify the positional matches and save them as uppercase in a variable that you can search for additional non-matched letters
  2. build de resulting string in a comprehension that will use the matched (uppercase) letter when the characters match at the same position, use the lowercase letter if it is present in the remaining (non-positional) match string otherwise an underscore:

def compWord(user,random):
    if len(user) != len(random): return
    matched = [r.upper() if r==u else r for u,r in zip(user,random)]
    return "".join( m if r==u else u if u in matched else "_"
                    for u,r,m in zip(user,random,matched))

output:

print(*compWord("elephant","elements")) # E L E _ _ _ n t
print(*compWord("migrants","elements")) # m _ _ _ _ N T S                  
print(*compWord("meetings","elements")) # m e E t _ N _ S

Note: This assumes that both words are provided in all lowercase letters.

Answered By: Alain T.
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.