Censor


#1

def censor(text,word):
        
    list1=text.split()
    
    x=len(word)
    a="*" * x
    
    for i in range(0, len(list1)):
        if i == word:
            i = a
    
    p=" ".join(list1)        
    return p
    print censor("hey hey hey","hey")

I don't know why my code isn't working. I have tried several ways with this exercise and a few others in this section and the grader simply doesn't accept any piece of code I write. Any help is appreciated


#2

Hi,

Here you loop through the integers from 0 to len(list1). You then check if any of those numbers are equal to the word variable. The world variable will be a string not an integer, so you will never find a match. I guess you ment to use list1[i] not just i.

Also

if i == word:
    i = a

i is your iterator, that takes on the value 0 then 1, and so on. Even if you set i = a, it will just take on the next integer value in the next iteration.

p=" ".join(list1)

You never made any changes to list1 so this will just be the original text.


#3

Thank you so much for pointing out what was going wrong. I have found that some rules that apply in IDLE don't apply when I'm writing the code on codecademy, especially when it comes to the 'for' loop, I get confused because IDLE doesn't recognise it when I type in something like list1[i]. Anyways, I find it difficult to debug my own code sometimes. So thanks!


#8