When calling the "countSubStringMatch" function in IDLE (I have called "countSubStringMatch('abcdaaa','a')" and expected the output would be 4) , the output was 1. I was really stuck at this. Could you help me explain clearly why the error occured? Thank you in advance

Replies To: Trouble with "While" and "For" Loop

Re: Trouble with "While" and "For" Loop

Posted 09 October 2012 - 12:53 AM

There should only be one while method and change your extra conditions to ifs. Also, just use equal on your while loop.

The error occurs since when you do your first while loop it will count the first appearance of 'a' then on the next run it is evaluated false, we move to the next loop where we check if the current character isn't equal to 'a', and we increment the counter. This evaluates true and loops until we encounter the next 'a', which then ends the while execution, ending the whole program.