10.Censor


#1



So what am i doing wrong,why adn how to fix.Also what to add and why.And is there a faster way to do this?
Error : Oops, try again. Your function fails on censor("hey hey hey","hey"). It returns "*** *** *** " when it should return "*** *** ***".


def censor(text, word):
    shit = ""
    sentence = ""
    text = text.split(" ") 
    for i in range(0, len(text)):
        if text[i] == word:
            shit = '*' * len(text[i])
            sentence += shit + " "
        else :
            sentence += text[i] + " "
            
    return sentence             
censor("this hack is wack hack", "hack")


#2

there is a additional space at the end of your sentence, you could just slice it off (sentence[:-1]) just before your return statement, which is the easiest solution, otherwise you would have to create a different solution


#3

I don't understand.If u mean the space between else and return functions i deleteed it and it didnt work


#4

no, the space at the end of your censored sentence. you return:

this_****_is_wack_****_

i replaced the spaces with underscores, there is a space after your censored string, i suggest you use slice to slice it off


#5

No idea what you are talking about and no idea what to do


#6

what your censored sentence is:

this_****_is_wack_****_

what it should be:

this_****_is_wack_****

i replaced the spaces with underscores, so you can see the spaces.

You either need to slice off the space at the end of the sentence, with slice like i showed, or you would have to rewrite your entire solution


#7

What do i have to do.I don't get it.What should should i do to my code exactly?How it should look like?


#8

i explained this thrice, you have to slice off the unnecessary space at the end of your censored sentence.

You should, just before you return your sentence, update the variable where the last character is sliced off:

sentence = sentence[:-1]

I can't do everything for you, there is a difference between helping and coding for you


#9

True.Ok thank you for helping.


#10

If you don't remember how slice works, you can always revisit the exercise where slice is explained, or check external documentation, where do you think you need to place slice? I would put nearly at the end of my function, just before the return sentence, since then your string is censored, and you want to cut off the unnecessary space before you return the censored string


#11

If I am having a lot of trouble with these practice makes perfect problems, where should I go back to. I find myself having to look for help on almost everyone except the first on since starting the practice makes perfect segment. Thank you in advance.


#12

They are tough challenges. That depends on what the problem is. Take a moment to realize what the problem is, think if you need a string, or list. Do i need list manipulation? String manipulation? Find those exercise. Look back over the exercise for some build in functions (split, join)

write some psuedo code (what are the steps?), write in plain english but in programming steps.

These challenges are designed to be taken slow, but to hugely improve your programming thinking skills


#13

Thank you =) This helps a lot.