Skip to content
Advertisement

python question about loops comparing string to string

I am trying to emulate a circumstance where i send information and only get a true or false as a return. So i can check each character and if it is true, that means that character is in the string. I would know there would be a position 0 to some number x. I would receive a true result and eventually only receive false result and then I would know the string has been solved. In my circumstance i would not know the target string.

I am trying to iterate through all characters and see if it matches the string character. if it does, I add the character to a list until the list contains all the characters of the string. but for some reason, this isn’t working.

JavaScript

results:

JavaScript

As you can see, it does not match the string

When I tried the code above, I was expecting the string to come out the same as the other string.

Advertisement

Answer

Based on my understanding of the question, I’ve implemented a function which I believe emulates the interface you are talking to:

JavaScript

This will return True and increment the counter into swoll when a character matches, otherwise it will return False.

You can then use this function in a loop which iterates until False is returned for all characters in characters. Inside the loop characters is iterated until a match is found, at which point it is added to hi:

JavaScript

Output for your sample data:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement