Advertisement
Guest User

Untitled

a guest
Feb 19th, 2018
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.33 KB | None | 0 0
  1. print("Type 'q' to quit")
  2.  
  3. while True:
  4.  
  5.     filename = input("Enter file name: ")
  6.     if filename == 'q':
  7.         break
  8.     else:
  9.         try:
  10.             with open(filename, encoding="utf8") as f:
  11.                 contents = f.read()
  12.  
  13.         except FileNotFoundError:
  14.             print("File not found.")
  15.  
  16.         else:
  17.             while True:
  18.                 word = input("Enter the word you want to count: ")
  19.                 word_count = contents.lower().count(word.lower())
  20.                 if word == 'q':
  21.                     raise SystemExit
  22.                 else:
  23.                     with open(filename, encoding="utf8") as f:
  24.                         contents = f.read()
  25.                         word_count = contents.lower().count(word.lower())
  26.  
  27.                     print("The word '" + word.lower() + "' appears " +
  28.                           str(word_count) + " times on this file.\n")
  29.  
  30.                     print("Type 'c' to change the file or 'a' to search " +
  31.                           "for another word")
  32.  
  33.                     command = input("Input the command: ")
  34.                     print()
  35.                     if command == 'c':
  36.                         break
  37.                     elif command == 'a':
  38.                         continue
  39.                     elif command == 'q':
  40.                         raise SystemExit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement