Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I move to the next index?

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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)}")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading