Assignment 7 - ldkvd/CS101L GitHub Wiki
Welcome to the CS101L Wiki!
Fuel Economy
In this lab, we’ll write a program to read through a file containing information about fuel economy and output the results to a file above a threshold that the user gives. If the user wants to see all vehicles with a combined mpg greater than 50, then your program will output that information to the file of their choosing. The information is tab-delimited. When you read a line from the file, the values are separated by the tab character \t. There are many ways to split this string data.
Your program will need to ask the user for a minimum fuel economy and be able to handle nonfloat data being entered. It should continually ask for a correct value. It should also make sure they don’t enter a value less than or equal to zero or greater than 100. The program will ask for an input file and should loop until the user gives a valid file that can be opened. It should also ask for an output file. While a FileNotFoundError won’t be thrown trying to open a file in write mode, it can generate an IOError.
When writing out to the output file, you should output the Year, Make, and Model left-justified by 40 spaces and the mpg right justified by 10 spaces. Your program should also be able to handle an incorrect combined mpg column as in vehicles2.txt. When a bad value that can’t be converted is encountered, you should warn the user and give the year, make and model that had the error.
Answer:
The image above is the source code part 1/3.
The image above is the source code part 2/3.
The image above is the source code part 3/3
This image above provides an output in which the values could not be converted. NOTE: On the assignment, input an output file of 'out*.txt' should return an IO Error. However, on MAC OS, symbols like an asterisk (*) is allowed in file name. So, I do not have an output for that.
This image above provides an output in which the values are outputted into the new file.
In this code, multiple functions are defined and called. Functions allow for the same piece of code to run multiple times. It reduces clutters, complexity, and duplication of codes. It breaks down a large program into smaller, easy-to-manage components. We can call one statement (the function), rather than writing the same code over and over for each time we want to encode or decode a plain text.
First, the function minimum_mpg() is called. It will ask the user to enter a number and run through a try and except block. Within the try statement, it will run through if-else statements to ensure that the value is valid or not and return the correct message. If the user input is not a float value, the program will run except ValueError block and print the error message. Then, the function open_file() is called. Within the try block, it will ask the user to enter the file that they want to open. It will open, read, and return the file. In the except block, it will return the respective error statement. When the function output_file() is called, it will ask the user to input the name of the file they would like to output to. The for loop will iterate through each row in the file and check for combined mpg. In the try block, if the combined mpg is greater than the user's input for the minimum mpg, then the program will write that value in the new output file. If the value is invalid, the except block will execute and return the error message. The files are then closed.