The computer use the text, "hey, hey, hey", and the word "hey". My program makes one "hey" ** but does not do it to the other "hey"s. Im not sure what to do to make it work. Its suppose to make every word in the text ""s
def censor(text, word):
text = text.split(" ")
for x in text:
if x == word:
cen_word = "*" * len(text)
Stetim thanks so much I see now why you were telling me to print. By doing that I see what each part of the code is doing. This is making more and more sense to me now. However, I am still a little stuck because I got the word to turn into asterisks, but im having trouble making it do it for the text. This is what i have so far:
" def censor(text, word): text = text.split() lst =  print text for x in range(len(text)): print x for x in text: if x == word: lst.append("*" * len(x)) print lst else: lst.append(word) return "".join(lst) censor("My name is Matt", "Matt")"
def censor(text, word): text = text.split() lst =  print text for x in range(len(text)): print x for x in text: if x == word: lst.append("*" * len(x)) print lst else: lst.append(x) return " ".join(lst) print censor("hey hey hey", "hey")
So breaking this down, the user puts in a string of text, and then the word they want to be censored.
Text.split breaks up the string into separate strings
an empty list is made to hold the future results
then like you said x becomes a number to index for each string
for each string in the text
if that string(X) is equal to the word to be censored, then append it to the empty list, replacing the length of the word, with asterisks.
Here is where I get lost. Why is the else needed? I feel as though it should never be called, however when messing around with the code, I take it out, and the code fails to work. So I was wondering when you get the chance if you could break down the significance of the else statement in this code or anything I messed up and didnt understand perviously above?
Thankyou again for helping me and not getting annoyed and having patience. Appreciate it so much!