我正在尝试使用<=
方法返回列表索引的句子长度split()
。如果我输入某个索引的确切长度,这可行,但<
似乎没有做任何事情。这是我的代码:
stories = [['With bloody hands, I say good-bye.'],
['TIME MACHINE REACHES FUTURE!!! ... nobody there ...'],
["Not In My Job Description: Make sure it's done by the end of the day Jones.\nBut, sir, it's not in my ....\nJust do it, and remember, no blood."]]
def len_sentence():
search = int(input("Enter int"))
for i in stories:
len1 = (i[0][0:].split(' '))
if len(len1) <= search:
print(i)
len_sentence()
整数1-5的用户输入不返回任何内容。如果我将<=
替换为>=
则可行。为什么<
没有做任何事情?
答案 0 :(得分:2)
您尝试的输入太小。你在那里最短的故事是6个字长,所以输入1-5不匹配任何东西。但代码对我来说很好,它只是你的搜索输入。