'beginner trying to recreate wordle and having trouble detecting when a letter is correct but in the wrong place
I've been trying to figure this out for a while but I'm having no luck here's what I'm trying
https://i.stack.imgur.com/WVqDu.png
this will cause it to only sometimes work for example it would show n was right but in the wrong place but with another word with n it would be wrong? currently, I have it so it will add it to a list that it will print that list is finalpr and I have it add a B in the corresponding spot in the finalpr list.
Solution 1:[1]
I would approach this by first checking for the right letter in the right place, by checking if the letter at an index i is the same in the guess and the word. Then using an elif statement means that the next block of code will only be run if the previous statement was not true, i.e. the letter is not correct and in the right place. Then it is only necessary to check if the letter is in the word, which can be done using the in keyword. Rather than writing each index from 1 to 5 individually, it will make your code more readable and quicker to write if you use a for loop, which will cause the variable i to take the values 0, 1, 2, 3 and 4.
finalpr = ["x", "x", "x", "x", "x"]
for i in range(5):
if guess[i] == word[i]:
finalpr[i] = "a" # Right letter, right place
elif guess[i] in word:
finalpr[i] = "b" # Right letter, wrong place
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Cameron Jones |
