Advertisement
funnybunnyofdoom

FileProgram

Mar 9th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 KB | None | 0 0
  1.  
  2.  
  3. def main():
  4. print("Welcome to the File Program.\n")
  5. valid = False
  6. while valid == False:
  7. selection = getChoice()
  8. if selection.lower() == 'p':
  9. fileObject = openFile()
  10. printReport(fileObject)
  11. valid = True
  12. elif selection.lower() == 'u':
  13. print("Opening an input file...")
  14. fileObject = openFile()
  15. print("Opening an ouput file...")
  16. secondFileObject = openFile()
  17. updateFile(fileObject,secondFileObject)
  18. valid = True
  19. else:
  20. print("Invalid Selection. Please choose again.\n")
  21. print("Would you like to restart the program? (Y/N)")
  22. choice = input()
  23. if choice.lower() == 'y':
  24. main()
  25.  
  26. def getChoice():
  27. print("Please choose an option:\n")
  28. print("P/p) Print Report")
  29. print("U/u) Update File")
  30. choice = input() #Store the input from user in the choice variable
  31. return choice #Return the choice to the caller
  32.  
  33. def openFile():
  34. print("Please enter the name of a file.")
  35. print("Hint: You must include .txt")
  36. path = input() #Get the file path/name
  37. valid = False #Store whether input was valid or not
  38. while valid == False: #While the input is invalid, loop. It starts out invalid.
  39. print("Please enter a file mode (w, a, or r)")
  40. filemode = input() #Get the file mode from the user.
  41. filemode = filemode.lower()#Change to lower case for use
  42. if filemode != 'w' and filemode != 'a' and filemode != 'r': #Check for invalid input
  43. print("Please enter a valid character")
  44. else: #If it's valid
  45. file = open(path,filemode) #Open the file with the filename(path) provided, and the file mode
  46. return file #Return the file object
  47.  
  48. def printReport(file):
  49. fileData = {}
  50.  
  51. index = 0
  52. place = 1
  53. total = 0
  54. totalprofit = 0
  55. for line in file:
  56. fileData[index] = line.strip('\n')
  57. if place == 1:
  58. print("Pizza: ",fileData[index])
  59. total +=1
  60. place = 2
  61. elif place == 2:
  62. print("Price: ",fileData[index])
  63. place = 3
  64. elif place == 3:
  65. print("Cost: ",fileData[index])
  66. place = 1
  67. op = index + 1
  68.  
  69.  
  70.  
  71. if op % 3 == 0 and index != 0: #Check for third number. add 1 to idex since it starts at 0
  72. price = float(fileData[index-1])
  73. cost = float(fileData[index])
  74. profit = price - cost #calculate the profit from price and cost
  75. totalprofit = totalprofit+profit #add to the total profit
  76. print("profit: $",profit,'\n')
  77. index = index + 1
  78. print("Reached the end of file" '\n')
  79. print("Total Pizzas Sold: ",total)
  80. while True:
  81. try:
  82. avg = totalprofit / total #Calculate the average
  83. break
  84. except ZeroDivisionError:
  85. print("You Cannot divide by Zero")
  86. return False
  87. print("Average profit per pizza: ",avg)
  88. file.close() #Close the file
  89.  
  90.  
  91. def updateFile(inputFile,outputFile):
  92. print("How many records would you like to write?")
  93. index = 1
  94. arr = {}
  95. writes = int(input()) * 3
  96. for line in inputFile:
  97. if index <= writes:
  98. arr[index] = line.strip('\n')
  99. print(arr[index])
  100. index +=1
  101. inputFile.close()
  102. for item in arr:
  103. outputFile.write(arr[item])
  104. outputFile.write('\n')
  105. outputFile.close()
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement