PYTHON   131

Untitled

Guest on 27th July 2022 01:25:06 AM

  1. # while true run everything in loop
  2. while True:
  3.     # make a default dictionary
  4.     dictionary = dict()
  5.     # make input to check file
  6.     check = input("Please enter file name to process: ")
  7.     # try to get file if not print error message
  8.     try:
  9.         # try open file
  10.         f = open(check, "r")
  11.         # replace "\n" with " "
  12.         text = f.read().replace("\n", " ")
  13.     except:
  14.         # if didnt work print error and go back in beginning
  15.         print("File name ", check, "doesn't exist.")
  16.         continue
  17.     # create for loop to scroll through words
  18.     for x in text:
  19.         # create for loop to scroll through letters
  20.         for i in x:
  21.             # check if i is already in dictionary if so add one to i(add one to the number value)
  22.             if i in dictionary:
  23.                 # adding 1 to number value
  24.                 dictionary[i] = dictionary[i] + 1
  25.             # if it isnt in it than create new variable and make it equal to 1
  26.             else:
  27.                 dictionary[i] = 1
  28.     # print final result
  29.     print(dictionary)
  30.     # input to see if user want to continue and lower it
  31.     c = input("Do you want to try another file? (y or n)").lower()
  32.     # if statement to see if user want to continue
  33.     # if not print bye and exit loop
  34.     # if c == y than restart loop
  35.     if c == "y":
  36.         continue
  37.     # if c == n than print bye and break out of loop
  38.     elif c == "n":
  39.         print("Thank you for playing.")
  40.         break

Raw Paste


Login or Register to edit or fork this paste. It's free.