COMI 1150 PROGRAMMING CONCEPTS LESSON 4 LAB READING TUITION DATA FROM A FILE In this lab, you’re going to open a file and read and process data from it in a loop. The file contains tuition data from...

needs to be done in python idle


COMI 1150 PROGRAMMING CONCEPTS LESSON 4 LAB READING TUITION DATA FROM A FILE In this lab, you’re going to open a file and read and process data from it in a loop. The file contains tuition data from some number of states, where every three lines are data for a single state of the form: State name 2002 public institution tuition 2012 public institution tuition You will process each record and produce output in the form of a table like the one below for the six New England states and a few statistics, including 2012 largest tuition, 2012 smallest tuition, average tuition, and largest increase. You may not assume that the file contains data, but you may assume it exists and you may assume each record of the file is complete. (In other words, if there is a state name, there will be the two lines after it of 2002 and 2012 tuition values.) State Name 2002 Tuition 2012 Tuition Change Connecticut 11066 19842 8776 Massachusetts 9361 20328 10967 Maine 10292 18631 8339 New Hampshire 12343 23314 10971 Rhode Island 11575 20649 9074 Vermont 13426 22504 9078 Think about how you would solve this problem before reading through the step-by-step instructions and hints below, and solve as much as you can on your own. You have learned everything you need to know to solve this problem in lessons 1-4. 1. OPEN YOUR PYTHON TEMPLATE AND RENAME IT. To begin, open your Python program template and give it a new name, such as lab4.py or state_tuition.py. Write a docstring at the top that describes what the program does and that includes the date. The basic outline of the program will be to open the file, process the file data and print table rows, and print the summary statistics, so write comments in the program for each of these logical blocks. 2. CREATE SEVERAL SAMPLE TEXT FILES IN A TEXT EDITOR. We will need to have a text file of sample data to work with. Because our program must work with a text file of any length, we should create several text files and test each version of our program on all of them. Below is the data for the six New England states shown in the table above. We can create a text file called six_states.txt, we can create an empty text file called empty.txt, and we can create a text file with just one state’s data, called one_state.txt. When we get to the testing step, we will create a few other variations. To create a text file, in IDLE, choose File -> New File, and when you save the file, choose “Text files (*.txt)” from the Save as type box. Be sure to save the text files in the same directory as your program. Copy/paste or type the data for the six states exactly as it is below for the six_states file, do not put anything in the empty file, and put only the three lines of data for Connecticut in the one state file. Connecticut 11066 19842 Massachusetts 9361 20328 Maine 10292 18631 New Hampshire 12343 23314 Rhode Island 11575 20649 Vermont 13426 22504 3. EDIT YOUR PYTHON PROGRAM TO OPEN THE TEXT FILE AND DETERMINE IF IT HAS ANY DATA. Now that you have some text files in the same directory as your program, you can open a text file with your program and read it. There are different ways to break this program up into smaller pieces to develop incrementally. You do not have to develop the program in the order it is developed here, but if you take a different development path, be sure that your program meets all of the requirements and gives correct output for all of the final test cases. We will begin by obtaining a file name from the user. This will make it easier for us to test our program on many test cases, as we can place the different test cases in different text files and simply type the name in at the prompt to test a specific test case. Once we have obtained the file name from the user, we will open the file. (We will assume the user has typed the name correctly and the file exists.) We will then read the first line from the file and check to see if it’s the end of file. If it isn’t, we’ll print the header for our table. If it is, we’ll print the message “ contains no data,” for example, “empty.txt contains no data.” Follow these steps: • Annotate variables for the file name (a string), the first line of the fi le (a string which wil l be either the state name or the end of f ile marker), and the file reference variable (of type typing.Text IO). You will have to import the typing l ibrary. Put the code for the import and the annotations under the correct comments in your program. Be sure to give the variables meaningful names and follow Python naming conv entions, such as beginning with a lowercase letter and separating words with underscores. For example, you might call the file reference variable tuition_file . • Use an input statement to obtain the fi le name from the user. Use an appropriate prompt, such as “Please enter the file name with the .txt extension: “ • Open the fi le and read the first line. • Use an if statement to determine if the first l ine is the end of file marker ( ""). I f it is, print the message that the file is empty. • If the file is not empty (use an else), print the table header. The table header is composed of four strings formatted in colum ns of 20 characters each. This is what the table header should look like: State Name 2002 Tuition 2012 Tuition Change If you can ’t remember how to use the format statement to do this, a hint is below. • Be sure to put comments over each block of code that relate the code below to the part of the problem being solved. • At the end of your program, close the tuit ion fi le. Formatting hint: This code prints the first two colum ns of the table header: print("{:20s}{:20s}".format("State Name","2002 Tuition")) If you run your program on the empty fi le and then on the file with six states, your executions should look like this: >>> RESTART: D:\ lesson4_lab_sample_solution.py Please enter the file name with the .txt extension: empty.txt empty.txt contains no data. >>> RESTART: D:\ lesson4_lab_sample_solution.py Please enter the file name with the .txt extension: six_states.txt State Name 2002 Tuition 2012 Tuition Change >>> 4. IF THE FILE ISN ’T EMPTY, READ RECORDS IN A LOOP UNTIL THE END OF FILE To produce the table with the state name, the two tuition values, and the change between the tuition values, we will have to read the records of state data one at a time. Think about how you read a multi-line record in a loop. A loop has an initializer, a loop entry condition, and an updater. The loop entry condition is whether we have reached the end of file. The initializer and updater read the first line of the record, and within the loop, we must read the rest of the record. See if you can write the loop before reading the hints below. Hints: • The loop control variable will hold the first line of the record – the state name variable. Either it’s a state name or it’s the end of file, which will control whether you enter the loop. You’ve already read it before we begin the loop because you
Mar 19, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here