Censor


#1

Hey guys, hope you are good, Why this code doest not work ? I solved it, but why this does not work ?? I want to know.

def censor(text, word):
    #text = raw_input("my \nname \nis \nname")
    #text = ""
    lst = []
    sent  = text.split()
    for x in sent:
        lst.append(x)
    
        for i in range(len(lst)):
            if sent[i] == word: 
        
               sent[i] = "*" * len(word)
               " ".join(sent)
    return sent

#2

you should do the join after the loop, and you have to store the result of this join somewhere or return the join directly


#3

actually what you know Stetim94, that was the change I made and it appeared correct. Thanks a lot. One more question, kindly look at the code below. this example is from count,

def count(sequence, item):
    lst = []
    
    found = 0
    
    for i in lst:
        
        if i == item:
            found += 1
    return found

when i make an empty list, like lst above and iterate over and increment and it gives an error without calling the function parameter at all. Could be stupid question, may disappoint you, sorry for that, but i want to know, Thanks in advance.


#4

Do you also understand why the change makes the code work?

the exercise calls the function for validation purposes. How else is it going to test your function works correctly?


#5

Yes now i got it, Thanks my friend


#6