10. 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 "*** *** ***".

def censor(text, word):
    for word in text:
        for l in word:
            word = word.replace(l, "*")
    return text

#2

word is a variable, changing what it refers to has no effect on the string that text refers to.


#3

So I made some changes with your hints and the lesson's hints in mind. I'm still getting error messages though.

'''
def censor(text, word):
text = text.split()
for item in text:
if item == word:
for l in item:
item.replace(l, "*")
text = " ".join(text)
return text
'''

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


#4

You can use print statements to find out whether each part of your code has the desired effect. Each time you identify something that isn't doing what you wanted you can describe the action you intended to uncle google - somebody else has probably asked about how to do that same thing

You seem to be trying to modify item.. but that's not part of the text, so that'll have no effect, that's pretty much the same as you were doing before.


#5

I'm using that advice to work on it, but as I've been tweaking things I'm now stuck on this error message:

Oops, try again. Does your censor function take exactly two arguments, a text string and a word to replace with asterisks? Your code threw a "list indices must be integers, not str" error.

I can't figure out what I'm doing wrong here. Here's my code:

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


#6

indices is plural for index. You're only using indexing in one place, and your error message is already saying what you're doing wrong with that


#7

I figured it out, thanks!


#8

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