In: Computer Science
hello please correct this code
print("The Miles Per Gallon program") print() Trips = [] trip = 0 while 1: print("Do you want to add a trip from a csv file or Enter it manually? 1 for csv 2 for entering it manually") method = int(input()) if method == 1: print("Enter the filename") fileName = input() try: with open(fileName, 'r') as myFile1: reader = csv.reader(myFile1) Trips = list(reader) print("Miles Driven Gallons Used \tMPG") for i in Trips: for j in i: print(j, end=" ") print() except IOError: print ("Could not read file:", fileName) elif method == 2: while 1: miles_driven = input("Enter miles driven: ") try: val = int(miles_driven) break except ValueError: print("No.. input string is not an Integer. It's a string") while 1: gallons_used = input("Enter gallons of gas used: ") try: val2 = int(gallons_used) break except ValueError: print("No.. input string is not an Integer. It's a string") mpg = val / val2 mpg = round(mpg, 2) Trips.append([]) Trips[trip].append(miles_driven) Trips[trip].append(gallons_used) Trips[trip].append(mpg) print("Miles Driven Gallons Used \tMPG") for i in Trips: for j in i: print(j, end= " ") print() trip += 1 choice = int(input("Do you want to add another trip? 1 for yes 0 for no ")) if choice == 1: continue elif choice == 0: break myFile = open('trips.csv', 'w') with myFile: writer = csv.writer(myFile) writer.writerows(Trips) with open('trips.csv', newline='') as myFile: reader = csv.reader(myFile) for row in reader: print(row) print("Elemnts from the csv file printed which means it was stored successfully")
i need result as
EXAMPLE RUN 1: - Bad filename C:\Files.py The Miles Per Gallon
program
Would you like to read trips from a file? y/n: y Enter the csv
filename containing trip data: test Trips not read from file - file
not found: test Would you like to enter trip data? y/n: y Enter
miles driven: 100 Enter gallons of gas used: 10 1. Miles: 100.0
Gallons of Gas: 10.0 Mpg: 10.0
Would you like to continue? y/n: y Enter miles driven: 50 Enter
gallons of gas used: 5 1. Miles: 100.0 Gallons of Gas: 10.0 Mpg:
10.0 2. Miles: 50.0 Gallons of Gas: 5.0 Mpg: 10.0
Would you like to continue? y/n: n
EXAMPLE RUN 2: Good filename and good inputs
C:\y The Miles Per Gallon program
Would you like to read trips from a file? y/n: y Enter the csv
filename containing trip data: trips.csv Trips: 1. Miles: 100.0
Gallons of Gas: 10.0 Mpg: 10.0 2. Miles: 50.0 Gallons of Gas: 5.0
Mpg: 10.0
Would you like to enter trip data? y/n: y Enter miles driven: 75
Enter gallons of gas used: 4 1. Miles: 100.0 Gallons of Gas: 10.0
Mpg: 10.0 2. Miles: 50.0 Gallons of Gas: 5.0 Mpg: 10.0 3. Miles:
75.0 Gallons of Gas: 4.0 Mpg: 18.75
Would you like to continue? y/n: n
c:\\
EXAMPLE RUN 3: Good Filename – bad user inputs
The Miles Per Gallon program
Would you like to read trips from a file? y/n: y Enter the csv
filename containing trip data: trips.csv Trips: 1. Miles: 100.0
Gallons of Gas: 10.0 Mpg: 10.0 2. Miles: 50.0 Gallons of Gas: 5.0
Mpg: 10.0 3. Miles: 75.0 Gallons of Gas: 4.0 Mpg: 18.75
Here is the fixed code
import csv
print("The Miles Per Gallon program")
print()
Trips = []
trip = 0
print("Do you want to add a trip from a csv file or Enter it manually? 1 for csv 2 for entering it manually")
method = int(input())
if method == 1:
print("Enter the filename")
fileName = input()
try:
with open(fileName, 'r') as myFile1:
reader = csv.reader(myFile1)
Trips = list(reader)
#print("Miles Driven Gallons Used \tMPG")
print("Trips Data found from the", fileName, "are\n")
#Trips: 1. Miles: 100.0 Gallons of Gas: 10.0 Mpg: 10.0 2. Miles: 50.0 Gallons of Gas: 5.0 Mpg: 10.0
for t in Trips:
trip = trip + 1
print("Trips: " + str(trip) +". Miles:",t[0],"Gallons of Gas:",t[1],"Mpg:",t[2])
except IOError:
# ("Could not read file:", fileName)
print("Trips not read from file - file not found:", fileName)
print("Would you like to enter trip data?")
while 1:
while 1:
miles_driven = input("Enter miles driven: ")
try:
val = int(miles_driven)
break
except ValueError:
print("No.. input string is not an Integer. It's a string")
while 1:
gallons_used = input("Enter gallons of gas used: ")
try:
val2 = int(gallons_used)
break
except ValueError:
print("No.. input string is not an Integer. It's a string")
mpg = val / val2
mpg = round(mpg, 2)
Trips.append([])
Trips[trip].append(miles_driven)
Trips[trip].append(gallons_used)
Trips[trip].append(mpg)
print("Miles Driven Gallons Used \tMPG")
for i in Trips:
for j in i:
print(j, end= "\t")
print()
trip += 1
choice = int(input("Do you want to add another trip? 1 for yes 0 for no "))
if choice == 1:
continue
elif choice == 0:
break
myFile = open('trips.csv', 'w')
with myFile:
writer = csv.writer(myFile)
writer.writerows(Trips)
with open('trips.csv', newline='') as myFile:
reader = csv.reader(myFile)
for row in reader:
print(row)
print("Elements from the csv file printed which means it was stored successfully")
See the image of python code:
Please copy the code from above. Python is an indentation based
language, any wrong indentation will cause code to fail. Due to
some technical issue the indentation in the code has lost. Hence I
request you to copy the code from above and indent it exactly as
per the given image.
Sample output:
Le - Te TUT TUULIU. Tervure input Do you want to add a trip from a csv file or Enterit manually? 1 for csv 2 for entering it manually Enter the filename trips.csv Trips Data found from the trips.csv are e Trips: 1. Miles: 100 Gallons of Gas: 10 Mpg: 10.0 Trips: 2. Miles: 300 Gallons of Gas: 32 Mpg: 9.38 Trips: 3. Miles: 300 Gallons of Gas: 4 Mpg: 75.0 Trips: 4. Miles: 350 Gallons of Gas: 3 Mpg: 116.67 Would you like to enter trip data? Enter miles driven: 100 Enter gallons of gas used: 2 Miles Driven Gallons Used MPG ct Us 100 10 10.0 300 32 9.38 300 75.0