Censor - Cant Figure Out Why Code Not Working


#1

def censor(text, word):
    new = text.split()
    new1 = ""
    for i in new:
        if i == word: 
            i = "*" * len(word)
            new1 += i
    return new1        

censor("hey hey hey", "hey")

#2

here:

     new = text.split()
    new1 = ""

why does the indent not match?

also, what happens to the spaces in your text string? your censoring doesn't seem to keep the spaces

What if a word doesn't need censoring? Currently, then nothing is happening


#3

First point i edited.

I dont understand your second point.


#4

well your censored string has no spaces between the words?

From the instructions:

For example:

censor("this hack is wack hack", "hack")

should return

"this **** is wack ****"

when the words (this, is and wack) don't need censoring, your code doesn't do anything with it?


#5

def censor(text, word):
    new = text.split()
    new1 = ""
    for i in new:
        if i == word:
            i = "*" * len(i)
            new1 += i + " 
    print new1
    return new1 

censor("hey hey hey", "hey")

Tried this, still no luck, adds a space at the end of *** *** *** and makes it wrong


#6

a space is part of a string, a string is always two quotation marks

this will create an additional space at the end of your censored string, you will need to slice it off.


#7

I'm very stupid, I don't understand how to do that at all.


#8

you used string slicing in pyglatin:

"hello world"[0:6]

#9

def censor(text, word):
    new = text.split()
    new1 = ""
    for i in new:
        if i == word:
            i = "*" * len(i)
            new1 += i + " "
            
    print new1
    return new1[0:len(new1)-1] 

censor("hey hey hey", "hey")

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


#10

i also already covered that problem?


#11

Can someone just post the correct code so I can go over it and figure out where mine is wrong please?


#12

asking and posting answers is a violation of the guideilnes

well, what is currently happening to word that does not require censoring? nothing, while we should add them to new1

if verifies if the words need censoring, else you can simply add the word + space


#13

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.