Answer: Provided in the explanation section
Step-by-step explanation:
Code to run this can be seen below:
# create list to store data from file
dates = [] # date of trip
fuel_used = [] # amount of fuel used on trip
miles_traveled = [] # number of miles traveled on trip
mileage = [] # mileage calculated
# open the input text file
file = open("trips.txt", "r") # open file in read only mode
# file contains each trip data on separate lines
# each data value is separated by comma
# read all data into arrays
while True:
line = file.readline() # read file line by line
# check for end of file
if line == "":
break
data = line.split(",") # separate each data from line
# fill list with data
dates.append(data[0])
fuel_used.append(int(data[1]))
miles_traveled.append(int(data[2]))
fuel = int(data[1])
miles = int(data[2])
mileage_calculated = miles / fuel
# add mileage into list
mileage.append(mileage_calculated)
# close the file
file.close()
# display data
print("{:15s}{:10s}{:10s}{:10s}".format("Date", "Fuel", "Miles", "Mileage"))
for i in range(len(dates)):
print(dates[i].ljust(15) + str(fuel_used[i]).ljust(10), end="")
print(str(miles_traveled[i]).ljust(10) + "{:.2f}".ljust(10).format(mileage[i]))
# open file to write data
file = open("fuel_mileage.txt", "w") # create a new file or overwrite existing one
# write data to file
for i in range(len(dates)):
trip_data = dates[i] + "," + str(fuel_used[i]) + "," + str(miles_traveled[i]) + "," + "{:.2f}\\".format(mileage[i])
file.write(trip_data)
# close the file
file.close()
cheers i hope this helped !!!