Question: Can someone help me finish this code? I believe I have most of it but I'm stuck. Here are the assignment instructions: Create a Python
Can someone help me finish this code? I believe I have most of it but I'm stuck.
Here are the assignment instructions:
Create a Python program that:
- Opens a file and reads the contents of the following file
- trips.txt
- The file contains several lines of data, with each data item separated by commas. The data is:
- A trip date
- The amount of fuel used on the trip
- The number of miles traveled on the trip
- For each of the lines of data in the file, separate the data items and put them in appropriate variables
- The file contents are in the form of strings, so you must convert the numbers in the data into the proper type
- The file contains several lines of data, with each data item separated by commas. The data is:
- trips.txt
- Create a function that calculates the fuel mileage achieved on each trip
- Pass the appropriate data to this function
- Place this function in a separate module
- Call the function as needed and have the function return the calculated fuel mileage
- Create a function that displays the following items on a single line, and place the function in the same module as the previous function.
- Pass the appropriate data to this function
- Trip data
- Amount of fuel used
- Numbers of miles traveled
- Fuel mileage calculated for the trip
- Call the function as needed to display the data items
- Pass the appropriate data to this function
- Create a new file to save the data read and the fuel mileage calculated for the trip
- Write the data on separate lines for each trip in the data, with each item separated by commas
- Remember that all of the data must be saved in the string form
The program should:
- Use variables with meaningful names
- Name the module containing the functions "SupportMod.py"
- Import the module into your program
- Display easily understood prompts when collecting user input
- Prompt the user for the name of the file to read the data from, and the name of the file to store the revised data
- Have appropriate comments indicating what each part of the program is doing
- Have a comment area at the beginning of the program identifying the author of the program and the class it was created for
Save the program and module with the functions as a Python modules, save them in zip file form, and submit the zip file through this assignment.
Here is the code I have so far:
'''Add module with functions to be used in this program''' import SupportMod
'''Variables''' readFileName = "" writeFileName = "" tripDate = "" tripMiles = 0.0 tripFuel = 0.0 tripMileage = 0.0 commaPosition = 0 lineLength = 0
'''File Handling''' readFileName = input("Please enter the name of the file you with to open: ") writeFileName = input("Please enter the name of the file you wish to creat: ")
readFileName += ".txt" writeFileName += ".txt"
readFile = open(readFileName, "r") #Open the file to read from it writeFile = open(writeFileName, "w") #Open the file to write to it
'''Loop to read from the file, extract the data, perform the computation, display the results, and write the results to a file.''' for line in readFile: commaPosition = line.find(",") #Find the first comma in the file line read lineLength = len(line) #Get the length of the file line read tripDate = line[0:commaPosition] #Extract the first data item from the line read line = line[commaPosition + 1: lineLength] #Slice the line string
commaPosition = line.find(",") #Find the next comma in the line read tripFiel = float (line[0:commaPosition]) #Extract the second data item from the line read
tripMiles = float (line[commaPosition + 1: lineLength]) #Slice the line string
tripMileage = SupportMod.Mileage(tripFuel, tripMiles) #Calculate the fuel mileage for the trip
SupportMod.ItemDisplay(tripDate, tripFuel, tripMiles, tripMileage)
writeFile.write(tripDate + "," + str(tripMiles) + "," + str(tripFuel) + "," + str(tripMileage)) format(tripMileage, ".2f") + " ") #Write the data items to the new file
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
