I want to move from guesser[0] to guesser[1] if the solution matches the first letter from guesser and so on but I just can't figure it out
import string
list = []
guesser = "bomba"
while True:
for characters in string.printable:
solution = ''.join(list) characters
print(solution)
if solution == guesser[0]:
list.append(solution)
break
I've tried
import string
list = []
guesser = "bomba"
index = 0
while True:
for characters in string.printable:
solution = ''.join(list) characters
print(solution)
if solution == guesser[index]:
list.append(solution)
index = 1
break
CodePudding user response:
The problem you're having is that with the code as written, you will never be able to match on any character of guesser
after the guesser[0] because solution
will be more than one character.
After it matches the first 'b', and index = 1, then it's going to be checking if guesser[1] (i.e. 'o') is == "bo", so it will never be true.
Try this:
import string
answer = []
guesser = "bomba"
while len(answer) != len(guesser): # We need this to end someday.
index = len(answer)
for character in string.printable:
if character == guesser[index]:
answer.append(character)
break # Exit the for loop
else: # If the for loop completes without finding a match
print(f"The character at guesser[{index}] is not in string.printable")
break # Exit the while loop
print(f"Answer found : {''.join(answer)}")