Showing error with correct code!


#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 "*** *** *** " when it should return "*** *** ***".


console print the correct value which is needed but codecademy showing the error


Replace this line with your code. 
text = raw_input('Enter: ')
word = raw_input('Enter: ') 

def censor(text,word):
    box=''
    split=text.split(' ')
    
    for i in split:
        if i == word:
            i='*'*len(word)
            box+=(i+' ')
            
        else:
            box+=(i+' ')
    return box
print censor(text,word)


#2

"*** *** *** "   // output of your function
"*** *** ***"    // correct output

Do you see the difference? Your function adds an extra space.


#3

what should i do then :confused:


#4

There are many solutions. You know that you have extra space at the end of the box, you can simply return box without the last character or you can remove the last character.

I would change box into the list. It solves the problem of the extra space:

text = raw_input('Enter: ')
word = raw_input('Enter: ') 

def censor(text,word):
    box=[]
    split=text.split(' ')
    
    for i in split:
        if i == word:
            i='*'*len(word)
        box.append(i)
    return ' '.join(box)

print censor(text,word)

#5

thanks bro ,,, it was really simple :grinning:


#6

You're very welcome! :slight_smile:


#7

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