class Cleaner:
def __init__(self, forbidden_word = "frack"):
""" Set the forbidden word """
self.word = forbidden_word
def clean_line(self, line):
"""Clean up a single string, replacing the forbidden word by *beep!*"""
found = line.find(self.word)
if found != -1 :
return line[:found] + "*beep!*" + line[found+len(self.word):]
return line
def clean(self, text):
for i in range(len(text)):
text[i] = self.clean_line(text[i])
example_text = [
"What the frack! I am not going",
"to honour that question with a response.",
"In fact, I think you should",
"get the fracking frack out of here!",
"Frack you!"
]
Привет всем, проблема со следующим кодом заключается в том, что при запуске я получаю следующий результат:
What the *beep!*! I am not going
to honour that question with a response.
In fact, I think you should
get the *beep!*ing frack out of here!
Frack you!
Во второй последней строке одна из "frack "не меняются.
Я пытался использовать метод In In, но это не работает с переменными.Так как же использовать оператор if, который отслеживает переменную вместо строки?но также меняет каждое слово, которое нужно изменить?
PS.его экзаменационная практика я сам не делал код.
Ожидаемый результат должен быть:
What the *beep!*! I am not going
to honour that question with a response.
In fact, I think you should
get the *beep!*ing *beep!* out of here!
Frack you!