Write python code with following instructions.
You will write a program that reads a data file. The data file contains ticket IDs and ticket prices. Your job is to read these tickets (and prices). find minimum, maximum and average ticket prices and output a report file. The report file should look exactly (or better than) the one attached (output.txt).
Input: A31 149.99 B31 49.99 A41 179.99 F31 169.99 A35 179.99 A44 169.99 open "input.txt" file using open() function. let variable f refer to the opened file.
2) create a list of strings (let's call this lines) where each item in the list represent a line in the file. You can do that by calling read() method followed by a split method. lines = f.read().split("\n") on some systems, you may need to do: lines = f.read().split("\r\n") make sure to close the file afterwards. f.close()
3) initialize variables total (with which we will accumulate the sum of prices in the input file), priceMin (the lowest priced ticket, initialized to a large value, ex. 99999), priceMax (the highest priced ticket initialized to a small value. ex. 0).
4) create a for loop that goes over each line in lines list. for line in lines:
5) the line contains two pieces of information, the seat and then the price with a space character in between. So let's split the line with space character to separate the price from seat designation. cols = line.split(" ")
6) at this point, cols[0] is the seat designation, and cols[1] is the price of this ticket. We can ignore the seat designation -- all we need is the price. Remember that everything we read from the file is a string. In order to work with price we need to convert it to a number. price = float(cols[1])
7) add price to total.
8) if price is larger than priceMax, set priceMax to price.
9) if price is smaller than priceMin, set priceMin to price.
10) Note that steps 5 to 9 are inside the for loop created at step 4. O
nce this loop runs, the total will be the sum of all ticket prices and priceMax and priceMin will contain the minimum and maximum prices. We can now output to output.txt, like this:
f = open("output.txt", "w") f.write("*******************************************\n") f.write(" TICKET REPORT\n") f.write("*******************************************\n\n") f.write("There are " + str(len(lines)) + " tickets in the database.\n\n") f.write("Maximum Ticket price is $" + str(priceMax) + "\n") f.write("Minimum Ticket price is $" + str(priceMin) + "\n") f.write("Average Ticket price is $" + str(total / len(lines)) + "\n\n") f.write("Thank you for using our ticket system!\n\n") f.write("*******************************************\n") f.close()
Students succeed in their courses by connecting and communicating with an expert until they receive help on their questions
Consult our trusted tutors.