What's wrong with my code?


#1

<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>

<Below this line, add a link to the EXACT exercise that you are stuck at.>
https://www.codecademy.com/courses/python-intermediate-en-rCQKw/1/4

<In what way does your code behave incorrectly? Include ALL error messages.>
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.

<What do you expect to happen instead?>
When the input is (‘hey hey hey’,‘hey’)
the expected result is ‘*** *** ***’
which is exactly the same as the result which was printed out

```python

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’)

<do not remove the three backticks above>

<img src="//codecademy-discourse.s3.amazonaws.com/original/4X/b/0/6/b06ac0ebe1ac9bf07d981e3796a69b546bba0566.png" width="645" height="500">

#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