7/15 help


#1

My code doesn't seem to work. Can anyone tell me what is wrong?
This is my code:

def censor(text, word):
text = ""
word = ""
for string in word:
word = ' **** '
return text + word


#2

I would also like any help I can get.


#3

What does the error message say?


#4

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


#5

Okay. Your code is setting both of the parameters to an empty string, and you actually don't need to reassign either of the parameters to anything. Also, you need to check if the word is in the text before you change it. You also want to replace the letters in the string with the same amount of asterisks (*'s). After you fixed those things, your code would still fail, because you are returning the whole text with the word replaced with four asterisks, and then the word after that, since Python will put the word after your text because your code says return text, then return the word, instead of just the text. Finally, you can do this in three lines of code

When I completed this exercise, my code looked like this:
def censor(text, word):
if word in text:
return text.replace(word, "*" * len(word))


#6

thanks. i'll look into that


#7

You're welcome. I had to look at the forums quite a bit to find the answer I was looking for when I did this


#8

I like looking at the archived forums


#9

There is a lot of stuff in there, and most of it is even helpful


#10

Plus, you can always find the funniest things there


#11

Try looping through the string, using a while loop. You can go through strings in reverse.

text[-1] = t, text[-2] = x etc

So, you can do the same in a function:

# reversing the order of a string:
def reverse(text):
    j = 1
    rev_text = ""
    while j < (len(text)+1):
        rev_text = rev_text + text[-j]
        j += 1
    return rev_text

text_2 = reverse("abalone")

#12
def reverse(text):
r_text = ''
index = len(text) - 1

while index >= 0:
    r_text += text[index] #string can be concatenated
    index -= 1

return r_text

print reverse("hello, world!")