10. Censor - What do I need to do to make this work



Your function fails on censor("hey hey hey","hey"). It returns "ey ey ey" when it should return "*** *** ***".

def censor(text, word):
    textc = ""
    for c in range(0,len(text)):
        if text[c] == word[0]:
            for i in range(0,len(word)):
                check = 0
                if text[c+i] == word[i]:
                    check = check + 1
                    if check == len(word):
                        textc = textc + "*" * len(word)
                    textc = textc + text[c]
            textc = textc + text[c]
    return textc


You would need to start by coming up with a series of operations that amount to getting the correct result.
Once you have that implemented, but it isn't doing what you expect, then you'll need to compare whether it's really doing those steps by observing how it runs. One way to do this is to add print statements which explain what it is doing step by step in good old plain English, which you can then check for mistakes and when you find the mistake you can look closer at the part of the code responsible for the failed operation.


Thanks! The advice about adding print statements was really helpful and my code now works perfectly.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.