Censor


#1



https://www.codecademy.com/courses/python-intermediate-en-rCQKw/1/4?curriculum_id=4f89dab3d788890003000096#

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


My reasoning was like this: make a list a, with all the apostrophes word has, by iterating through it; then the final sentence, beginning as a split list derived from text, has an x correspondent to word replaced by the written equivalent of a, and is then converted into text again.

My guess is that the problem is related to the ability of the program to assign to variable x within the if statement. Am I right?


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


#2

the problem is that here:

for x in sent:

x will contain read-only values from the list, any changes made to x won't persist, given the loop will assign the next value from the list in the following iteration

How do you think we can overcome this problem?


#3

Hmm, yeah, I see it now... as for the solution, I think it has to do with removing x and putting the string on its index - quick question, how is it again that I view the index of x?


#4

we can update elements in a list using indexes, range() is the function you learned for this


#6

[EDIT] Wow, emails show up here too?...

Oh yeah, I was just returning to this today and noticed your email. Just
looking at the first lines gave me an epiphany, and I wrote the rest by
myself:

‚Äč

Thanks for the help anyway!


#7

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