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


#1



https://www.codecademy.com/courses/python-intermediate-en-rCQKw/1/4?curriculum_id=4f89dab3d788890003000096#


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)
                else:
                    textc = textc + text[c]
        else:
            textc = textc + text[c]
    return textc


#2

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.


#3

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


#4

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