count calls the find function to find out how many times a letter can be found in a word starting at a given index (see "code" below).
The confusing part: Using the "count" function, I get the following program output: 
As you can see, some outputs are duplicated (marked in red). How can this be avoided without removing the fingerprint from the search? Is it possible, or am I forced to remove it (seal)? I understand that these two functions can be turned into simpler ones, but I want to understand how to call a function using the other.
I should also mention that the value of the variable counter is correct. The only problems are duplicated outputs.
Code:
def find(word, letter, index): start_ind = index while index < (len(word)): if word[index] == letter: print "%s found at index %s" % (letter, index) return index index += 1 else: print "%s is not found in string '%s' when starting from index %s" % (letter, word, start_ind) return -1 def count(word, letter, index): count = 0 while index < len(word): if find(word, letter, index) != -1: count += 1 index = find(word, letter, index) + 1 print "%s is shown %s times in '%s'" % (letter, count, word) count("banana", "a", 0)
source share