Problem statement : text와 word 문자열을 입력으로 사용하는 censor라는 함수를 작성하십시오. 그것은이 훨씬 더 복잡보다PYTHON IndexError : 문자열 인덱스가 범위를 벗어났습니다.
def censor(text, word):
i = 0
j = 0
ans = ""
while i<len(text):
while text[j] == word[j]:
j = j + 1
if text[j+1] == " " or j+1 == len(text):
while i<j:
ans += "*"
i = i + 1
ans += " "
i = i + 1
else:
while text[j] != " ":
j = j + 1
while i<=j:
ans += text[i]
i = i + 1
i = i + 1
j = j + 1
return ans
print censor("how are you? you are not fine.","you")
그러나 나는 다음과 같은 오류가 발생하고,
Traceback (most recent call last):
File "python", line 27, in <module>
File "python", line 7, in censor
IndexError: string index out of range
내가 코드 카데미에 문제를했던뿐만 아니라 : 귀하의 경우 , 당신은 단어의 길이의 사용과 텍스트의 단어를 일치하도록해야한다. 이것은 실제로 복잡하지 않아도됩니다. –