What's wrong with my code?


#1



https://www.codecademy.com/courses/python-intermediate-en-rCQKw/1/4


Before I return the result, I print it out and it's the same as the expected result. But, it said I was wrong since the result of my function is not the same as the expected result. And I am confused.


When the input is ('hey hey hey','hey')
the expected result is '*** *** ***'
which is exactly the same as the result which was printed out


def censor(text, word):
    textl = list(text)
    wordl = list(word)
    for i1 in range(len(textl)-len(wordl)):
        #print textl[i1:i1 + len(wordl)]
        status = cmp(textl[i1:i1 + len(wordl)], wordl)
        print status
        if status == 0:
            for i2 in range(len(wordl)):
                print textl[i1+i2]
                textl[i1+i2] = '*'
                print textl[i1+i2]
    print ''.join(textl)
    return ''.join(textl)
censor('hey hey hey ', 'hey')


#3

nice approach, I made it a different way and I think yours is better (at least shorter - after you remove the print commands).

you get error when you pass a text = 'heyhey' which will result in'***hey' and not '******' as expected. the problem is in your line 4 where you subtract the lenghts of your words, in above example this will be 6 - 3 so range will create a list of 0, 1, 2 whereas you should have a list of 0, 1, 2, 3 to make that example pass. so a slight code correction is needed.


#4

thank you for your reply