Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def main():
- print("Welcome to the File Program.\n")
- valid = False
- while valid == False:
- selection = getChoice()
- if selection.lower() == 'p':
- fileObject = openFile()
- printReport(fileObject)
- valid = True
- elif selection.lower() == 'u':
- print("Opening an input file...")
- fileObject = openFile()
- print("Opening an ouput file...")
- secondFileObject = openFile()
- updateFile(fileObject,secondFileObject)
- valid = True
- else:
- print("Invalid Selection. Please choose again.\n")
- print("Would you like to restart the program? (Y/N)")
- choice = input()
- if choice.lower() == 'y':
- main()
- def getChoice():
- print("Please choose an option:\n")
- print("P/p) Print Report")
- print("U/u) Update File")
- choice = input() #Store the input from user in the choice variable
- return choice #Return the choice to the caller
- def openFile():
- print("Please enter the name of a file.")
- print("Hint: You must include .txt")
- path = input() #Get the file path/name
- valid = False #Store whether input was valid or not
- while valid == False: #While the input is invalid, loop. It starts out invalid.
- print("Please enter a file mode (w, a, or r)")
- filemode = input() #Get the file mode from the user.
- filemode = filemode.lower()#Change to lower case for use
- if filemode != 'w' and filemode != 'a' and filemode != 'r': #Check for invalid input
- print("Please enter a valid character")
- else: #If it's valid
- file = open(path,filemode) #Open the file with the filename(path) provided, and the file mode
- return file #Return the file object
- def printReport(file):
- fileData = {}
- index = 0
- place = 1
- total = 0
- totalprofit = 0
- for line in file:
- fileData[index] = line.strip('\n')
- if place == 1:
- print("Pizza: ",fileData[index])
- total +=1
- place = 2
- elif place == 2:
- print("Price: ",fileData[index])
- place = 3
- elif place == 3:
- print("Cost: ",fileData[index])
- place = 1
- op = index + 1
- if op % 3 == 0 and index != 0: #Check for third number. add 1 to idex since it starts at 0
- price = float(fileData[index-1])
- cost = float(fileData[index])
- profit = price - cost #calculate the profit from price and cost
- totalprofit = totalprofit+profit #add to the total profit
- print("profit: $",profit,'\n')
- index = index + 1
- print("Reached the end of file" '\n')
- print("Total Pizzas Sold: ",total)
- while True:
- try:
- avg = totalprofit / total #Calculate the average
- break
- except ZeroDivisionError:
- print("You Cannot divide by Zero")
- return False
- print("Average profit per pizza: ",avg)
- file.close() #Close the file
- def updateFile(inputFile,outputFile):
- print("How many records would you like to write?")
- index = 1
- arr = {}
- writes = int(input()) * 3
- for line in inputFile:
- if index <= writes:
- arr[index] = line.strip('\n')
- print(arr[index])
- index +=1
- inputFile.close()
- for item in arr:
- outputFile.write(arr[item])
- outputFile.write('\n')
- outputFile.close()
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement