Censor


#1



I've almost done this function and it works correctly but there's an error > "Oops, try again. Your function fails on censor("hey hey hey","hey"). It returns " 123 *** 123" when it should return "*** *** ***"." ( 123 321 123 was my previous try)
and > Oops, try again. Your function fails on censor("hey hey hey","hey"). It returns " *** *** ***" when it should return "*** *** ***". if i try to use ("hey, hey, hey")
i understand that there's something wrong with order of strings, but i don't know where exactly


t = raw_input().lower()
w = raw_input().lower()
def censor(text,word):
    t1 = t.split()
    
    t2 = "*" * len(w)
    w1 = w
    total = ""
    for i in t1:
        if i not in w:
            total += " " + i
        else:
            i = t2
            total += " " + i
        
    return total


#2

lets add a function call:

# censor function here

# getting user input
t = raw_input().lower()
w = raw_input().lower()
# calling the function
print censor(t, w)

now t and w are copied into the function parameters (text and word) so inside the function use text and word, not t and w

This way, your function works for any string you pass as argument when calling the function


#3

def censor(text,word):
    t1 = text.split()
    
    t2 = "*" * len(word)
    w1 = word
    total = ""
    for i in t1:
        if i not in word:
            total += " " + i
        else:
            i = t2
            total += " " + i
        
    return total

t = raw_input("text").lower()
w = raw_input("censor").lower()

print censor(t,w)

I tried but there's the same mistake


#4

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

Oh well i think i know why there's the mistake, it's because of " "before ***


#5

you are much closer to solving the problem now. Lets replace the spaces with underscores:

_***_***_***

see how you have a space at the beginning at the string which you shouldn't have, how do you think you can solve this?


#6

I've used - return " ".join(total) but it make space after every letter so i used" " + i
now i guess " ".join(total) was the right choice but i can't imagine how to make spaces only between words instead of letters


#7

if you want to use join, total should be a list. Join is designed to join a string into a list

The other option is to use string slicing to remove the space in the beginning


#8

Thank you very much slicing worked great
I am very grateful to you for helping me all this time, your advices make me understand it much better

def censor(text,word):
    t1 = text.split()
    
    t2 = "*" * len(word)
    w1 = word
    total = ""
    for i in t1:
        if i not in word:
            total += " " + i
        else:
            i = t2
            total += " " + i
        
    return total[1:]
    

t = raw_input("text").lower()
w = raw_input("censor").lower()

print censor(t,w)

#9

you're welcome

If you feel like you want a challenge, change total to a list:

total = []

append to total, and use join, this is more efficient (strings are immutable in python)

also, here:

if i not in word:

i now contains words (thanks to split), so you can simply check if i equals word (or not equals)


#10

    total = []
    for i in t1:
        if i not in word:
            total.append(i)
        else:
            i = t2
            total.append(i)
        
    return " ".join(total)

Great, it think looks more proper


#11

i am still not understand why you use not in here, instead of not equal (!=), but yes, it is most certainly an improvement


#14

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