Censoring is an endless job


i can't seem to understand why this code doesn't word, seems to get into an infinite loop :frowning:

def censor(text,word):
temp_w = ""
temp_t = ""
leng = len(word)
for letter in text:
while letter.isalpha():
temp_w = temp_w + letter
temp_t = temp_t + letter
if temp_w == word:
temp_t = temp_t + asterisk(temp_w)
temp_w = ""
temp_t = temp_t + letter
temp_w = ""
return temp_t

def asterisk(word):
for i in len(word):
astword = astword + "*"
return astword


There is your infinite loop. letter is always alpha.


Thanks for your answer!
I was under the impression that isalpha() is false for punctuation and spaces...
Is there another way to check this condition?


You have the correct impression. Only alpha characters are accepted.

Two come to mind, filter() and regular expressions. Will need to do some digging.


I'm sorry, I think I'm lost :slight_smile:
if the while loop returns true for the letters, and then when reaches space/punctuation characters goes to "else" and goes on with the function, how is it infinite?


The conditional state of a while loop must eventually reach False or a forced break at some point. Using a while loop to perform logic is tricky, and can easily lead to infinite loops if we do not make the exit state the first priority of the construct.

More importantly, we need to get the lay of the land and measure up the problem before possible solutions begin to surface. If a solution is getting complicated, then it is time to step back, save your work, and start again. Some times a fresh start helps us stumble upon or discover through serendipity where we were going wrong.

We only need to match word units, not characters. The punctuation or white space in the text should not be a concern. str.split() removes the white space, and returns a list of terms. The SCT is not going to throw any punctuation at the function so we can safely forego that concern, but it is a valid one, just the same.

Now if we are going to design a method that strips all non-alpha from a string it would be sensible to work with the list generated by str.split() since we want to keep the words separate from one another and not just a bunch of scrunched up text with no spaces or special characters.


x = "one's effort, is; its. own, reward."

x = x.split()

for term in x:
    i = x.index(term)
    new_term = []
    for c in term:
        if c.isalpha():
    x[i] = ''.join(new_term)
x = ' '.join(x)

print x

# ones effort is its own reward

The above is using what we know, up to this point. We can find other approaches, and together can keep exploring. Just know this is over and above what the exercise calls for; it is a self-learning experience for added practice. Keep up your track studies so you don't lose focus.

I only advise that because toward the end of the track is a unit on advanced topics that includes some of the ingenious built-in methods available to us in Python, two of which are filter() and lambda.

The following segment,

    for c in term:
        if c.isalpha():

may be rewritten as,

    new_term.append(filter(lambda c: c.isalpha(), term))

The differences are subtle, yet profound. Study them. The latter contains a pure function which definition I'll leave you to look up. You'll not be expected to master this here, but your future employer or project demands might expect this at the very least. There is a lot to learn and master. Don't expect the moon to fall in your lap. Reach for it.

This may be further simplified, thus,

for term in x:
    i = x.index(term)
    x[i] = ''.join(filter(lambda a: a.isalpha(), term))
x = ' '.join(x)

print x

Censor not working!

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