Censor output looks ok but Codecademy doesn't accept it?


#1



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


Morning guys,
I seem to be having an issue with the system accepting my answer. With the trouble shooting lines I've put in it gives the right out put but codecademy doesn't seem to recognize it. Any ideas why?

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


word = "lame"
text = "You are a lame person"

def censor(text,word):
    
    censored = len(word) * "*"
    input_array = text.split(" ")
    output_string = ""
    
    for x in input_array:
        if x == word:
            output_string += censored + " "
        else:
            output_string += x + " "
    print output_string
    return output_string
    
censor(text,word)


#2

You have an extra space at the end of your output.

your output:
"*** *** *** "
they want:
"*** *** ***"

You should create cases (before the others) that checks if the word, x, is at the end of the input_array. And if it is, you should not add " " to your output_string.

Additional 2 cases to include:
1. If x is last item and is a word to be censored, just do output_string += censored.
2. If x is last item and is not a word to be censored, just do output_string += x.


#3

Thanks for the help. Instead of the IF statements I used .rstrip to clean it up prior to output and it was accepted.

word = "hey"
text = "hey hey hey"

def censor(text,word):
    
    censored = len(word) * "*"
    input_array = text.split(" ")
    output_string = ""
    
    for x in input_array:
        if x == word:
            output_string += censored + " "
        else:
            output_string += x + " "
    output_string = output_string.rstrip()
    print output_string
    return output_string
    
censor(text,word)

#4

Oh, cool! I never knew about rstrip() so I learned something. Great job :relaxed:


#5

I believe all you needed to do was to remove the "print output_string" because all you need to do is to return the function. I have had these errors many times and this seems to work.


#6

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