Question

Write a Python program that computes the fuel efficiency of a multi-leg journey. The program will...

Write a Python program that computes the fuel efficiency of a multi-leg journey. The program will first prompt for input for the starting odometer reading and then get information about the series of legs on the journey. For each leg, the user enters the current odometer reading and the amount of fuel used in liters (separated by a blank space). The user signals the end of the trip with a blank line. The program should print out the kilometers per liter achieved on each leg (to 1 decimal precision). The program should also print out for the trip the total distance, total fuel consumed, and fuel efficiency for the entire trip.

See the example below:

>>>Enter initial odometer reading: 1000
>>>Enter leg info (odometer reading, fuel used): 1040 2
20.0 km per liter
>>>Enter leg info (odometer reading, fuel used): 1080 3
13.3 km per liter
>>>Enter leg info (odometer reading, fuel used): 1190 10
11.0 km per liter
>>>Enter leg info (odometer reading, fuel used): 1250 5
12.0 km per liter
>>>Enter leg info (odometer reading, fuel used):
Total distance traveled = 250 km. Total fuel used = 20 liters. Fuel efficiency = 12.5 km per liter.

Using PyCharm and please use basic while, for and if loop.

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

Code starts here :

initial = int(input("Enter initial odometer reading: "))
input_values = input("Enter leg info (odometer reading, fuel used): ")
last, total_fuel = initial,  0
while input_values != "":
    reading, fuel = (int(x) for x in input_values.split())
    print(round((reading-last)/fuel, 1), "km per liter")
    total_fuel += fuel
    input_values = input("Enter leg info (odometer reading, fuel used): ")
    last = reading
print("Total distance traveled =", last-initial, "km. Total fuel used =", total_fuel, "liters. Fuel Efficiency =", round((last-initial)/total_fuel, 1), "km per liter.")

We first take initial odometer reading and declare variables for maintaining last reading, total_fuel and current reading and current fuel.

We maintain a while loop with condition that input should not be empty. If it is empty, then loop will break, and final outputs wiil be printed.

On line 5, the code splits the input into two, reading and fuel, and converts into integer value.

round() function will round the float value to the given precision, here 1.

Add a comment
Answer #2

Can you provide answer for this question


answered by: harichandana dhanapuneni
Add a comment
Know the answer?
Add Answer to:
Write a Python program that computes the fuel efficiency of a multi-leg journey. The program will...
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
  • Ive done this part but i want to know how i can change the file so...

    Ive done this part but i want to know how i can change the file so it accepts inputs and a file input PLEASE HELP PYTHON Write a program that computes the fuel efficiency of a multi-leg journey. The program will first prompt for the starting odometer reading and then get information about a series of legs. For each leg, the user enters the current odometer reading and the amount of gas used (separated by a space). The user signals...

  • 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...

  • 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...

  • 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...

  • 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...

  • Write program PSEUDOCODE that prompts the user for their name and then will compute a car's...

    Write program PSEUDOCODE that prompts the user for their name and then will compute a car's miles per gallon given input of miles driven and gallons of gas used. It should also convert those given numbers into metric and show kilometers driven, liters used, and kilometers per liter. All with well formatted output. Submit: C++ Programming Warm-Up Exercise PSEUDOCODE for the algorithm that compute's a car's miles per gallon given input of miles and convert these numbers into metric and...

  • 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...

  • PLEASE DO THIS IN PYTHON!! 1.) Goal: Become familiar with The Python Interpreter and IDLE. Use...

    PLEASE DO THIS IN PYTHON!! 1.) Goal: Become familiar with The Python Interpreter and IDLE. Use IDLE to type, compile, and run (execute) the following programs. Name and save each program using the class name. Save all programs in one folder, call it Lab1-Practices. Save all programs for future reference as they illustrate some programming features and syntax that you may use in other labs and assignments. ================================== Program CountDown.py =================================== # Program CountDown.py # Demonstrate how to print to...

  • Shopping Cart You are to write a program that reads the name of the item, the quantity of each it...

    C++ language Shopping Cart You are to write a program that reads the name of the item, the quantity of each item, and the cost per item from a file. The program will then print out the item and how much is owed for each item. After everything is read in, it will print out the total cost of the cart. Your program should have the following functions main - This function asks the user to enter a filename and...

  • Use program control statements in the following exercises: Question 1 . Write pseudocode for the following:...

    Use program control statements in the following exercises: Question 1 . Write pseudocode for the following: • Input a time in seconds. • Convert this time to hours, minutes, and seconds and print the result as shown in the following example: 2 300 seconds converts to 0 hours, 38 minutes, 20 seconds. Question 2. The voting for a company chairperson is recorded by entering the numbers 1 to 5 at the keyboard, depending on which of the five candidates secured...

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