Advertisement
Guest User

working lab 2.py

a guest
Sep 16th, 2019
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.98 KB | None | 0 0
  1. import time
  2. import string
  3. # global F_Contents Corrected - was F_Content, moved inside function
  4.  
  5.  
  6. def F_info():
  7.  
  8.     global space_count
  9.     global F_Contents
  10.     global F_hand
  11.     F_hand = ""
  12.     file = ""
  13.     F_hand = input("\nWhat file would you like to find?\n")
  14.     if not F_hand.endswith(".txt"):
  15.         F_hand += ".txt"
  16.     print("\nAttempting to open:", F_hand, "\n")
  17.     time.sleep(0.4)  
  18.     try:
  19.         with open(F_hand, "r") as file:
  20.             F_Contents = file.read()
  21.             print(F_Contents)
  22.     except:
  23.         print("\nfile does not exist\nplease enter a valid file name\n")
  24.         F_info()
  25.  
  26.  
  27. def opchoice():
  28.     print ("------------------------------------------------------------")
  29.     operation = input(
  30. """\n\nwhich of the following would you like to do?\nA: Count all letters\nB: Count all words\nC: Count how many times a specific letter appears\nD: Count how many times a specific word or phrase appears\nE: View entire file in uppercase letters\nF: View entire file in lowercase letters\nG: Exit the program\n""").upper()
  31.     print ("------------------------------------------------------------")
  32.     if operation == "A":
  33.         with open (F_hand, "r") as Afile:
  34.             data = Afile.read().replace(" ","").replace("+","").replace("×","").replace("÷","").replace("=","").replace("%","").replace("/","").replace("*","").replace("€","").replace("£","").replace("@","").replace("$","").replace("","").replace("","").replace("","").replace('',"").replace("!","").replace("#","").replace(":","").replace(";","").replace("&","").replace("_","").replace("(","").replace(")","").replace("-","").replace("'","").replace('"',"").replace(",","").replace(".","").replace("?","").replace("1","").replace("2","").replace("3","").replace("4","").replace("5","").replace("6","").replace("7","").replace("8","").replace("9","").replace("0","")
  35.             Alen = len(data)
  36.             print (
  37.     "\nThe total number of letters is: ",Alen,"\n"
  38.         )
  39. #part of a bigger program but this operarion choice
  40. #should find the number of words in a file
  41. #without counting any special characters, numbers, or #individual letters
  42.     elif operation == "B":
  43. #W_num is number of words
  44. #F_Contents is whats inside the file
  45.         W_num = sum([i.strip(string.punctuation).isalpha() for i in F_Contents.split()])
  46.            
  47.         print ("The number of words is:",W_num,"\n")   
  48. #Should only output number of words, but for me it is #counting individual letters as words too
  49.     elif operation == "C":
  50.         letter = input("\nEnter a letter\n").lower()
  51.         Al_char = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","1","2","3","4","5","6","7","8","9","0"]
  52.         while letter not in Al_char:
  53.             letter = input("Choose a single letter or number\n").lower()
  54.         L_count = (F_Contents.lower().count(letter))
  55.         print ("The letter",letter,"appears",L_count,"times")
  56.        
  57.     elif operation == "D":
  58.         phrase = input("\nEnter a word or phrase\n").lower()
  59.         while (len(phrase)) <= 1:
  60.             phrase = input("\nenter a valid word or phrase or series of letters\n").lower()
  61.         ph_count = F_Contents.lower().count(phrase)  
  62.         print(ph_count)  
  63.  
  64.  
  65.     elif operation == "E":
  66.         print(F_Contents.upper())  
  67.  
  68.    
  69.     elif operation == "F":
  70.         print(F_Contents.lower())  
  71.    
  72.     elif operation == "G" or "EXIT" or "QUIT" or "DONE":
  73.         exit()
  74.    
  75.     else:
  76.         print("Please choose a valid operation from the list of operations.(A-G)")
  77.         opchoice()
  78. def retry():
  79.     choice = input("Would you like to find another file? (y/n)\nIf you would like to use the same file and use a new\n operation instead enter (same)\n").lower()
  80.     if choice == "y":
  81.         F_info()
  82.         opchoice()
  83.         retry()
  84.     elif choice == "same":
  85.         opchoice()
  86.         retry()
  87.     else:
  88.         exit()
  89. with open("testfile.txt", "w+") as test:
  90.     test.write("The dog walked across the room.")
  91. with open("testfile.txt", "r") as test:
  92.     contents = test.read()
  93.     print("checking contents of testfile.txt:\n", contents)
  94.  
  95. with open("breaktest.txt","w+") as test2:
  96.     test2.write("+ w d ×%+/÷ *€× h &#? hello '1 572 2 ()$_")
  97. with open("breaktest.txt","r") as test2:
  98.     contents2 = test2.read()
  99.     print ("checking contents of breaktest.txt\n", contents2)
  100.        
  101.  
  102. F_info()
  103. opchoice()
  104. retry()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement