#python #slide
#python #слайд
Вопрос:
Я хочу сдвинуть строку, но вывод программы выдает ошибку.
Traceback (most recent call last):
File "<tmp 3>", line 28, in <module>
print_hell(sentence)
File "<tmp 3>", line 23, in print_hell
print(i)
TypeError: 'list' object is not callable
Что мне делать?
»’
sentence = "hello, how are you doing. I am doing good how are you. the work I am doing is hellish. please help me escape this hell. I will try my best. But oh hell no. this is too hard"
def string2list(text):
''' devide the list into smaller interval for easy to work '''
final_text = []
k = text.split(".")
for i in range(len(k)):
v = k[i].split()
final_text.append(v)
# return something like this [["",""],["",""]]
return final_text
def print_hell(text):
"print the sentence with the word 'hell', even the word like 'hello' with hell in it "
khn = string2list(text)
for i in range(len(khn)):
for l in range(len(khn[i])):
for c in range(len(khn[i][l])-4):
# 4 because the word hell has 4
if khn[i][l][c:4] == "hell":
print(i)
if __name__ == '__main__':
#print(string2list(text))
print_hell(sentence)
# v = string2list(text)
# print(text[1][2])
»’
Спасибо,
Ответ №1:
essay = "print the sentence with the word 'hell'. Even the word like 'hello' with hell in it. And even hellooo. Not this though "
list_of_sentences = essay.split(".")
for sentence in list_of_sentences:
if "hell" in sentence.lower():
print(sentence)
Таким образом, вы сначала преобразуете эссе в список предложений, а затем проверяете каждое предложение в списке на наличие в нем сочетания букв «ад».
Комментарии:
1. но это не напечатало бы предложение, содержащее слово «привет», не так ли (в world hello также есть «ад». Я хочу написать это даже для большого эссе, а не только для нескольких предложений.
2. На самом деле, попробуйте, он напечатает «привет», если вы передадите его.
3. Попробуйте, и если это сработает, пожалуйста, примите мой ответ как правильный
4. в строке печати по-прежнему написано «TypeError: объект ‘list’ не вызывается»
5. Я только что протестировал это снова, у меня это работает… вы ничего не изменили?