Censor ! help!


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


Hey, may i just check what I did wrong here that it doesnt loop through every word in the sentence? sorry i'm really bad at this.


def censor(text,word):
    splits = text.split()
    
    for key in splits:
        if key == word:
            key = "*" * len(word)
            return key
        else:
            return key
    return ''.join(splits) 
    
print censor( "hey hey hey","hey")


#2

@saltyjim,
Use the following code
and have a close look at what you are doing !!!!

def censor(text,word):
    splits = text.split()
    print("  text: {0}".format(text))
    print("splits: {0}".format(splits))
    print("  word: {0} ==> {1}".format(word,"*" * len(word)))
    print("=============================")
    
    for key in splits:
        print("   key: {0}".format(key))
        if key == word:
            key = "*" * len(word)
            # a =return= statement EXITS the function
            #return key
        #ELSE is not needed, 
        #as you can insert key directly into =result= list 
        #else:
        #    return key
        #====================
        # here you want to insert =key= into =result= list
        print("newkey: {0}".format(key))
        print("========")
    #=return= the =result= list and NOT original =splits= list
    return ''.join(splits) 
    
print censor( "hey hey hey","hey")

#3

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