sentence= ("ask not what your country can do for you ask what you can do for your country")
keyword= input("Input a keyword from the sentence")
words = sentence.split(' ')
for i, word in enumerate(words):
if keyword == word:
print(i+1)
else:
print("the word,",keyword,",is not in the sentence")
如果没有else语句,将打印该单词在句子中的位置,但是,一旦添加了else语句,即使该单词在句子中“打印”字样,“关键字”,“不在句子中”。其他语句不适用于我的if语句
第一次'keyword'不等于'word'时,你的else语句被执行。 –