Question

In: Computer Science

hello please correct this code print("The Miles Per Gallon program") print() Trips = [] trip =...

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

Solutions

Expert Solution

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


Related Solutions

Miles per Gallon. The following stem-leaf plot is representing the number of miles per gallon achieved...
Miles per Gallon. The following stem-leaf plot is representing the number of miles per gallon achieved on the highway for 2013 small car models. Construct an Ogive of data by first construction a cumulative frequency table with class width of five. Include: Limits, frequency, and cumulative frequency. 2 2       means 22 miles per gallon 2 2 2 5 7 9 9 9 9 9 3 0 0 0 0 0 1 1 1 2 2 2 2 2 3 3...
you currently drive 300 miles per week in a car that gets 20 miles per gallon...
you currently drive 300 miles per week in a car that gets 20 miles per gallon of gas. you are considering buying a new fuel-efficient car for $12,000 ( after trade-in on your current car) that gets 50 miles per gallon. Insurance premiums for the new and old car are $900 and $500 per year, respectively. you anticipate spending $1300 per year on repairs for the old car and having no repairs on the new car. assume gas costs $3.50...
Your car gets 22 miles per gallon (MPG) at 55 miles per hour (MPH) and 18...
Your car gets 22 miles per gallon (MPG) at 55 miles per hour (MPH) and 18 MPG at 65 MPH. At what speed should you make a 450-mile trip 1. If gas costs $2.95 per gallon and your time is worth $17/hour? 2. If gas costs $3.80 per gallon and your time is worth $11.5/hour? 3. If gas costs $4.75 per gallon and your time is worth $8.9/hour? 4. Building an Excel spreadsheet to calculate the total trip cost for...
Your car gets 25 miles per gallon (mpg) at 60 miles per hour (mph) and 18...
Your car gets 25 miles per gallon (mpg) at 60 miles per hour (mph) and 18 mpg at 70 mph. At what speed should you make a 600-mile trip: 1. If gas costs $3 per gallon and your time is worth $12 per hour? 2. If gas costs $4 per gallon and your time is worth $15 per hour? 3. If gas costs $5 per gallon and your time is worth $10 per hour? 4. Build a spreadsheet to calculate...
Your car gets 29 miles per gallon (mpg) at 60 miles per hour (mph) and 25...
Your car gets 29 miles per gallon (mpg) at 60 miles per hour (mph) and 25 mpg a 70 mph. At what speed should you make a 525-mile trip: If gas costs $3 per gallon and your time is worth $18 per hour If gas costs $4 per gallon and your time is worth $12 per hour If gas costs $5 per gallon and your time is worth $9 per hour
Suppose gas costs $3 a gallon and the average car gets 28 miles per gallon. If...
Suppose gas costs $3 a gallon and the average car gets 28 miles per gallon. If Congress mandates that cars have to get 36 miles per gallon, by what percentage will this lower the costs of driving? If the elasticity of total miles driven (per year) with respect to the cost of driving is –1, by how much will total miles driven per year increase, assuming it is 10,000 miles at the beginning? How much will total annual gas consumption...
Suppose gas costs $3 a gallon and the average car gets 28 miles per gallon. If...
Suppose gas costs $3 a gallon and the average car gets 28 miles per gallon. If Congress mandates that cars have to get 36 miles per gallon, by what percentage will this lower the costs of driving? If the elasticity of total miles driven (per year) with respect to the cost of driving is –1, by how much will total miles driven per year increase, assuming it is 10,000 miles at the beginning? How much will total annual gas consumption...
The accompanying data represent the miles per gallon of a random sample of cars with a​...
The accompanying data represent the miles per gallon of a random sample of cars with a​ three-cylinder, 1.0 liter engine. ​(a) Compute the​ z-score corresponding to the individual who obtained 41.4 miles per gallon. Interpret this result. ​(b) Determine the quartiles. ​(c) Compute and interpret the interquartile​ range, IQR. ​(d) Determine the lower and upper fences. Are there any​ outliers? 32.7 34.0 34.7 35.4 36.0 36.2 37.3 37.6 37.7 37.9 38.1 38.5 38.6 39.0 39.2 39.4 39.9 40.7 41.4 41.8...
It is necessary for an automobile producer to estimate the number of miles per gallon (mpg)...
It is necessary for an automobile producer to estimate the number of miles per gallon (mpg) achieved by its cars. Suppose that the sample mean for a random sample of 5050 cars is 30.630.6 mpg and assume the standard deviation is 3.63.6 mpg. Now suppose the car producer wants to test the hypothesis that μμ, the mean number of miles per gallon, is 31.631.6 against the alternative hypothesis that it is not 31.631.6. Conduct a test using a significance level...
It is necessary for an automobile producer to estimate the number of miles per gallon (mpg)...
It is necessary for an automobile producer to estimate the number of miles per gallon (mpg) achieved by its cars. Suppose that the sample mean for a random sample of 5050 cars is 30.630.6 mpg and assume the standard deviation is 3.63.6 mpg. Now suppose the car producer wants to test the hypothesis that μμ, the mean number of miles per gallon, is 31.631.6 against the alternative hypothesis that it is not 31.631.6. Conduct a test using a significance level...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT