Practice makes perfect: Censor


#1




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)
    return cen_word


#2

the easiest options are:

use range(), this will give you indexes which you can then use to substitutes words in text

create a new empty list before the loop, use if/else to append the words to the empty list, censor when the word require censoring.

currently, your code will create one variable called cen_word and censor the words, but overwriting cen_word every time, so you only end up with one censored word


#3

So I would keep the for loop though to iterate through the text right? Im gonna try and put it altogether right now. Dunno why Im struggling with this one so much. Thanks stetim ill give it a try now.


#4

good luck! If you need more help post an updated version of your code

I highly recommend to add a a function call, then you can add print statements inside the function and see what is happening.


#5

how would you do that? where would you put it?


#6

the function call has only spot?

The print statements? Anywhere you want to ensure your code is running smoothly, and see what is happening at which point, that is for you to decide.


#7

thankyou. I hope I can get as good at this as you understand this stuff haha


#8

this must be possible, i used to know nothing about programming. So it can be learned :slight_smile:


#9

Im trying to use the range option like you said but i keep getting int and str errors. Im really frustrated haha i think its making it worse cause i cant think clearly.


#10

can i see an updated version of your code? Maybe i can push you in the right direction


#11

I ended up erasing it. Let me try one more time and if i get stuck ill post it sorry i should of kept it on the screen


#12

ok can you help me a bit. so this is what I have so far,

def censor(text, word):
    text = text.split()
    lst = []
    for x in range(len(text)):

I wanna write next, " for x in text", or if " x == word" but I feel like thats gonna lead in error. Sorry to be a pain lol


#13

okay, so then we do this:

def censor(text, word):
    text = text.split()
    lst = []
    for x in range(len(text)):
        print x
censor("hey hey hey", "hey")

so x is a number. which so happens to match with the indexes of the list in text. You remember how to access list with numbers? so we can use x to access the words in list.

This is useful for both the if condition and censoring a word


#14

you access by calling the list[:] right? if im wrong ill go back and re read that section


#15

print ["python","ruby","javascript"][0:2]

would print: ["python","ruby"], if you want a single value (which we want) you can just do:

print ["python","ruby","javascript"][0]

which will give python


#17

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


#19

I got it! Thank you for your help

here is my finished code

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


#20

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!


#21

exactly, that is why i let you figure it out, because you need to do this yourself to see the value.

text.split() breaks up the string into a list, each item in the list is a word

because else append the uncensored words to the new list? How else do the uncensored words get into the list for future results.

it seems i have confused, if you go for the if/else approach you can simply do:

def censor(text, word):
    text = text.split()
    lst = []
    for x in text:
        if x == word:
            lst.append("*"*len(word))
        else:
           lst.append(x)
    return " ".join(lst)

range() was used if you wanted to use substitute:

def censor(text, word):
    text = text.split()
    for x in range(len(text)):
        if text[x] == word:
            text[x] = "*" * len(word)
    return " ".join(text)

this way, you can substitute items in the list with the censored word if needs be

You could argue that it would be optimizing to do this:

def censor(text, word):
    star = "*" * len(word)
    text = text.split()
    for x in range(len(text)):
        if text[x] == word:
            text[x] = star
    return " ".join(text)

now it only has to create the string and check the word of length once.


#22

Ok, see I guess im confused on else part because I thought "else" only got called if the "if" statement was not true. For example, x = 2, if x == 2, do something, else, do this?