Question

The Python program will compute total mileage for all trips included : Trip: 1,2,3,4 Mileage: 200,...

The Python program will compute total mileage for all trips included :

Trip: 1,2,3,4

Mileage: 200, 400, 800, 10

Gas: 10, 17, 39, 1

As well as the fuel efficiency in miles per gallon.

Your program will open the file, read it line by line and add the miles driven and the gallons gas consumed then calculate the average fuel efficiency. It will then write

Once the program has read all the lines, it will write the total miles driven, total gas consumed and the gas miles per gallon to a file called output.txt.

You will assume that your txt files are located in the same directory as the code to avoid path issues.

Remember that any open file will need to be closed.

Example of execution:

opening file trip.txt

opening file output.txt

output.txt will contain:

total mileage: 1410

total gas consumed: 67

overall mile per gallon: 21.04

0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any doubts, please give me comment...

Code:

def readline(s, ind):

return list(map(int, s.strip()[ind:].split(",")))

def main():

fp = open("trips.txt", "r")

print("opening trips.txt")

trips = readline(fp.readline(), 6)

mileages = readline(fp.readline(), 9)

gas = readline(fp.readline(), 5)

fp.close()

fout = open("output.txt", "w")

print("opening output.txt")

total_mileage = 0

for mile in mileages:

total_mileage += mile

total_gas_consumed = 0

for consumed_gas in gas:

total_gas_consumed += consumed_gas

milePerGallon = total_mileage/total_gas_consumed

print("total mileage:",total_mileage, file=fout)

print("total gas consumed:",total_gas_consumed, file=fout)

print("overall mile per gallon: %.2f"%milePerGallon, file=fout)

print("Successfully wrote to output.txt")

fout.close()

main()

Add a comment
Know the answer?
Add Answer to:
The Python program will compute total mileage for all trips included : Trip: 1,2,3,4 Mileage: 200,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a Python program that does the following: Obtains the following input from a user: Mileage...

    Write a Python program that does the following: Obtains the following input from a user: Mileage at beginning of measurement period Mileage at end of measurement period Gallons of fuel consumed Calculates and displays: Miles driven Miles per gallon Kilometers driven Liters of fuel consumed Kilometers per liter Incorporate some Selection structure logic into it: If a vehicle gets less than 15 miles per gallon, display the message:       "Your vehicle has criminally low fuel efficiency." If it gets 15...

  • Write a Python program that does the following: Obtains the following input from a user: Mileage...

    Write a Python program that does the following: Obtains the following input from a user: Mileage at beginning of measurement period Mileage at end of measurement period Gallons of fuel consumed Calculates and displays: Miles driven Miles per gallon Kilometers driven Liters of fuel consumed Kilometers per liter Also incorporate some selection structure logic into it. If a vehicle gets less than 15 miles per gallon, display the message:       "Your vehicle has criminally low fuel efficiency." If it gets...

  • C++, Visual Studio 3. Define a class called Odometer that will be used to track fuel...

    C++, Visual Studio 3. Define a class called Odometer that will be used to track fuel and mileage for an automobile. The class should have instance variables to track the miles driven and the fuel efficiency of the vehicle in miles per gallon. Include a mutator method to reset the odometer to zero miles, a mutator method to set the fuel efficiency, a mutator method that accepts miles driven for a trip and adds it to the odometer's total, and...

  • I need trying to figure out how I can make create a code in Python with this exercise for I am having trouble doing so. Miles Per Gallon Calculator Write a GUI program that calculates a car's gas...

    I need trying to figure out how I can make create a code in Python with this exercise for I am having trouble doing so. Miles Per Gallon Calculator Write a GUI program that calculates a car's gas mileage. The program's window should have Entry widgets that let the user enter the number of gallons of gas the car holds, and the number of miles it can be driven on a full tank. When a Calculate MPG button is clicked,...

  • python code DC Final Project Implement a class Car with the following properties. A car has...

    python code DC Final Project Implement a class Car with the following properties. A car has a certain fuel efficiency (measured in miles/gallon) and a certain amount of fuel in the gas tank. The efficiency is specified in the constructor, and the initial fuel level is 0. The following two lines are written in a File called FuelEffic.txt (you have to read these from the txt file) Miles per gallon: 20 Tank Size (in gallons): 25 Therefore, based on these...

  • Write a program to compute miles per gallon of a car, over a long trip. The...

    Write a program to compute miles per gallon of a car, over a long trip. The car begins the trip with a full tank, and each fuel stop along the way fills the tank again. You will not do any math, arithmetic, or computations in the main() function. All output will be displayed from the main() function. Start the program by asking the user for the starting odometer reading. All odometer readings will be in whole miles only. The fuel...

  • You previously wrote a program that calculates and displays: Miles driven Miles per gallon Kilometers driven...

    You previously wrote a program that calculates and displays: Miles driven Miles per gallon Kilometers driven Liters of fuel consumed Kilometers per liter Messages commenting on a vehicle's fuel efficiency And also validates input Now we'll make a somewhat major change. We'll remove the code for obtaining input from a user and replace it with hardcoded data stored in lists. Because the data is hardcoded, the program will no longer need the code for validating input. So, you may remove...

  • A liter is 0.264179 gallons. Write a program that will read in the number of liters...

    A liter is 0.264179 gallons. Write a program that will read in the number of liters of gasoline consumed by the user's car and the number of miles per gallon the car delivered. Your program should allow the user to repeat this calculation as often as the user wishes. Define a function to compute the number of miles per gallon. Your program should use a globally defined constant for the number of liters per gallon. After doing this... Modify your...

  • In JAVA In this assignment you will use a class Car to represent a car that travels to various de...

    In JAVA In this assignment you will use a class Car to represent a car that travels to various destinations. Your car has a fuel economy rating of 32.3 miles per gallon. The gas tank holds 19.5 gallons. Your program will need to simulate two trips: 1) BC to Yosemite Valley, and 2) BC to Washington, D.C.. For each trip you will start with a full tank of gas. The output should look as follows. Trip one: Bakersfield College to...

  • Drivers are concerned with the mileage their automobiles get. One driver has kept track of several...

    Drivers are concerned with the mileage their automobiles get. One driver has kept track of several tankfuls of gasoline by recording the miles driven and gallons used for each tankful. Develop a C# app that will input the miles driven and gallons used (both as integers) for each tankful. The app should calculate and display the miles per gallon obtained for each tankful and display the combined miles per gallon obtained for all tankfuls up to this point. All averaging...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT