Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help writing python code with following instructions. You will write a program that reads a data file. The data file contains ticket IDs

I need help writing 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(" ") on some systems, you may need to do: lines = f.read().split(" ") 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. Once 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("******************************************* ") f.write(" TICKET REPORT ") f.write("******************************************* ") f.write("There are " + str(len(lines)) + " tickets in the database. ") f.write("Maximum Ticket price is $" + str(priceMax) + " ") f.write("Minimum Ticket price is $" + str(priceMin) + " ") f.write("Average Ticket price is $" + str(total / len(lines)) + " ") f.write("Thank you for using our ticket system! ") f.write("******************************************* ") f.close()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Joe Celkos Data And Databases Concepts In Practice

Authors: Joe Celko

1st Edition

1558604324, 978-1558604322

More Books

Students also viewed these Databases questions

Question

In what way does the rule of doubt help copyright applicants?

Answered: 1 week ago