Censor - may i have a qustion?


#1

my question is: how to upgrade code, if in text input will be at least two same words (to censore) and we want to replace both these words with afterrisks. Not only one. Thanks guys:)

My code((It has been wrote in python3):

text = str(input("Type a sentence: "))
word = str(input("Type a word to being censored: ")) #define inputs

texts = text.split(" ")                 #split a string to the list
wposition = texts.index(word)    #find a position of word in text and save in variable
for item in texts:                       #iterate over texts list
    if item == word:                   #test condition for word input
        item = "*" * len(item)        #redefine variable item if condition is passed
        texts[wposition] = item     #replace choosen word in list with censored item

print(' '.join(texts))                     #print output

#2

There are a couple of options, to stay as close to your code as possible, is to use range() in the for loop, this will give you indexes so you can replace words in texts when they require censoring

The problem is that index() only gives you the first index


#3

Thanks,

i am going to think about it>]....


#4

good luck, if you get stuck and need more help with this problem, you can post updated version of your code + question

One tip: add print statements so you can see what your code is doing


#5

Update form today:))))
any other advice? I am not able to take myself from place...
what was meant "use range"???

text = str(input("Type a sentence: "))
word = str(input("Type a word to being censored: ")) #define inputs


texts = text.split(" ")           #split a string to the list
censored = "*" * len(word)
for item in texts:               #iterate over texts list
    if item == word:             #test condition for word input
        item = censored        #redefine variable item if condition is passed
print(item)
print(' '.join(texts))

#6

texts = ["just", "an", "example"]
for i in range(len(texts)):
    print i
    print texts[i]

using range will give you a list of numbers, which if we do it right, matches the indexes of texts


#7

yes, I understand this but not able to use it....


#8

Why are you not able to use range()?

you can use range() in the for loop you currently have?


#9

texts = text.split(" ")       
censored = "*" * len(word)
for item in range(len(texts)):            
    if item == word:          
        item = censored  
print(texts[item])
print(item)
print(' '.join(texts))

#10

remember that item now contains indexes:

texts = text.split(" ")       
censored = "*" * len(word)
for item in range(len(texts)): 
    print item

You can use the indexes to access items from the list:

print texts[item]

you need this for the comparison. And to update your texts lists with your censored word


#11

thanks i gonna find algorithm...


#12

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