10 Censor


#1




Can someone explain what is going on? The word does not get censored at all. I think the problem is in line 5 words = '*' * len(words). It seems to not be replacing the chosen word in the list with the asterisks.


def censor(text, word):
    text = text.split()
    for words in text:
        if words == word:
            words = '*' * len(words)
    text1 = " ".join(text)
    return text1
    
print censor("bjd aeivdn efsd ws", "bjd")


#2

here:

for words in text:

words gets assigned each word in text, but this won't persist. each iteration of the loop words gets overwritten with the next word from text.

if you want to manipulate the text list, use range() in your for loop, this will give indexes, which you can use to update items at certain indexes in a list


#5

I think for words in text: will take each letter of the text items & next if words == word: will compare those letters with the full "word" which is not true..so throughout all the iteration for all letters in text the if statement will not execute and finally after joining the text it will return the same input text.. :frowning:


#6

for astrosloth1? No, because here:

text = text.split()

he updates text by splitting it, so text is now a list with the words, so now looping over it would give the words


#7

you have to call each word with the index. Some lessons before there is written "for c in text" you are not able to modify "c". i solved in this way


#8

Whenever I try that it gives me an error message:

TypeError: object of type 'int' has no len()


#9

please show us how you did this


#12

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