lundi 14 novembre 2016

Else statement not working with my if statement

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")

Without the else statement the place where the word is in the sentence will be printed, however, once I added the else statement was added, even if the word is in the sentence "the word,",keyword,",is not in the sentence" is printed.

Aucun commentaire:

Enregistrer un commentaire