234. Python Program to Calculate the Fuel Cost of a Trip
EasyPhase 1 · Warm-up
This is two small sums with input(), float() and :.2f. You know all of them.
Overview
Before a long drive, people like to know how much the fuel will cost. Two small sums give the answer.
Problem Statement
Priya plans a road trip. Priya knows 3 numbers: the distance, how many kilometres the car goes on 1 litre of fuel, and the price of 1 litre.
Read these 3 numbers. Print how many litres the trip needs and what the fuel costs, both with 2 decimal places, in this format:
Litres: <litres>
Cost: <cost> coins
Steps
- Read the distance, the km per litre and the price as decimal numbers.
- Work out the litres: the distance divided by the km per litre.
- Work out the cost: the litres times the price.
- Print both with 2 decimal places.
Examples
Example 1. Input:
300
15
2
gives:
Litres: 20.00
Cost: 40.00 coins
Why: 300 ÷ 15 = 20 litres, and 20 × 2 = 40 coins.
Example 2. Input:
100
8
1.5
gives:
Litres: 12.50
Cost: 18.75 coins
Why: 100 ÷ 8 = 12.5 litres, and 12.5 × 1.5 = 18.75 coins.
Input
Three lines:
- The distance in kilometres (0 or more).
- The kilometres the car goes on 1 litre (more than 0).
- The price of 1 litre in coins (more than 0).
Each number may have a decimal point. Use input() with no prompt text: the tests type the input for you, and a prompt would end up in your output.
Output
Two lines: Litres: <litres> and Cost: <cost> coins, both with 2 decimal places. Work out the cost from the full litres, not from the rounded number. The tests try 6 different inputs, also a distance of 0.
Try it first. The solution opens when you solve this question, or after 3 tries with Submit.