Question: Exercise 7 - 3 Keep trip data in a binary file In this exercise, you'll modify the programs that you created for exercises 7 -

Exercise 7-3 Keep trip data in a binary file
In this exercise, you'll modify the programs that you created for exercises 7-1
and 7-2 so they create and use a binary file instead of a CSV file. Otherwise,
everything should work the same.
Modify the CSV version of the write program
Open the mpg_write.py file that you created in exercise in 7-1. Then, save it as
mpg_write_binary.py in the same directory.
Modify this program so it saves the list as a binary file instead of a CSV file.
The file should be named trips.bin.
Test the program to make sure it works. To do that, add statements that read
the file at the end of the program and display the list that has been read.
Modify the CSV version of the trip program
Open the
mpg.py file that you created in exercise 7-2. Then, save it as
mpg_binary.py.
Modify this program so it works the same as it did with the CSV file.
Test this program to make sure it works.
Here is the non-modified version of "mpg_binary.py":
#!/usr/bin/env python3
def get_miles_driven():
while True:
try:
miles_driven = float(input("Enter miles driven:\t "))
if miles_driven >0:
return miles_driven
else:
print("Entry must be greater than zero. Please try again.
")
except ValueError:
print("Invalid input. Please enter a valid number.
")
def get_gallons_used():
while True:
try:
gallons_used = float(input("Enter gallons of gas:\t "))
if gallons_used >0:
return gallons_used
else:
print("Entry must be greater than zero. Please try again.
")
except ValueError:
print("Invalid input. Please enter a valid number.
")
def write_trips(trips):
with open('trips.csv','w') as file:
for trip in trips:
file.write(','.join(map(str, trip))+'
')
def read_trips():
trips =[]
try:
with open('trips.csv','r') as file:
for line in file:
trip_data = line.strip().split(',')
trips.append([float(data) for data in trip_data])
except FileNotFoundError:
pass # If file doesn't exist yet, return empty list
return trips
def list_trips(trips):
print("Distance\tGallons\t\tMPG")
for trip in trips:
print(f"{trip[0]}\t\t{trip[1]}\t\t{trip[2]}")
def main():
print("The Miles Per Gallon program
")
trips = read_trips()
list_trips(trips)
more ="y"
while more.lower()=="y":
miles_driven = get_miles_driven()
gallons_used = get_gallons_used()
mpg = round((miles_driven / gallons_used),2)
print("Miles Per Gallon:\t "+ str(mpg))
print()
trips.append([miles_driven, gallons_used, mpg])
write_trips(trips)
more = input("More entries? (y or n): ")
print("Bye")
if __name__=="__main__":
main()
Here is the non-modified version of "mpg_write_binary.py":
#!/usr/bin/env python3
import csv
def get_miles_driven():
while True:
miles_driven = float(input("Enter miles driven: "))
if miles_driven >0:
return miles_driven
else:
print("Entry must be greater than zero. Please try again.
")
def get_gallons_used():
while True:
gallons_used = float(input("Enter gallons of gas used: "))
if gallons_used >0:
return gallons_used
else:
print("Entry must be greater than zero. Please try again.
")
def save_to_csv(trips):
with open('trips.csv','w', newline='') as file:
writer = csv.writer(file)
writer.writerow(['Distance', 'Gallons', 'MPG'])
for trip in trips:
writer.writerow(trip)
def main():
print("The Miles Per Gallon application
")
trips =[]
while True:
miles_driven = get_miles_driven()
gallons_used = get_gallons_used()
mpg = round(miles_driven / gallons_used, 2)
# Display the calculated MPG
print("Miles Per Gallon: {:.2f}".format(mpg))
# Append trip data to the list
trips.append([miles_driven, gallons_used, mpg])
more = input("
More entries? (y or n): ")
if more.lower()!="y":
break
# Save the trip data to a CSV file
save_to_csv(trips)
print("Trip data saved to trips.csv
")
print("Bye")
if __name__=="__main__":
main()
I am asking for the modified versions of "mpg_binary.py" and "mpg_write_binary.py" shown here. That includes fully written codes from the modified versions of "mpg_binary.py" and "mpg_write_binary.py".
Exercise 7 - 3 Keep trip data in a binary file In

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!