furas

Python - for/break/else - (FB: learnpython.org)

Jan 11th, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.39 KB | None | 0 0
  1. sentence = input("Enter a sentence: ")
  2.  
  3. keyword = input("Input a keyword from the sentence: ")
  4.  
  5. words = sentence.split()
  6.  
  7. for i, word in enumerate(words, 1):
  8.     if keyword.lower() == word.lower():
  9.         print("This word appears (first time) in sentence in position", i)
  10.         # keyword found so leave `for` loop
  11.         break
  12. else: # there was no break
  13.     print("not in sentence")
Add Comment
Please, Sign In to add comment