Image of the problem:
Code involved:
def checkguess(self,guess):
self.guess="".join(self.guess)
print(self.guess)
for i,letter in enumerate(self.guess):
if self.answer[i] == self.guess[i]:
self.highlightbox(i,GREEN)
elif letter in self.answer:
self.highlightbox(i,YELLOW)
else:
self.highlightbox(i,GRAY)
I have tried to use breaks, I think it involves making a second loop, and putting the check inside the loop, and then breaking the loop once it goes to a correct letter.
Create a copy of
answer
or better create acollections.Counter
from it. In a first loop check for exact matches (green). For each exact match set the letter in theanswer
copy to e.g.-
or decrement it in theCounter
. In a second loop check remaining places of guess for remaining characters in theanswer
. Again, if a match is found, remove the character or decrement theCounter
.You could 1st make a pass for green letters, replacing each one with some symbol in self.answer (or rather a copy of it), then make a 2nd pass for yellow letter with the same logic (else you’ll get the same problem).