← Intro to programming with Python
Mini project: road-trip fuel cost
Ask for trip details and work out what the fuel will cost.
Now combine input, conversion, math and f-strings. A program that works out fuel cost needs three facts: how far you will drive, how far the car goes on one litre (or gallon), and the price of fuel. The plan is: read each fact, convert it, compute litres needed, then compute the cost.
distance = float(input("Trip length (km): "))
per_litre = float(input("Km per litre: "))
litres = distance / per_litre
print(f"You need {litres:.1f} litres")If the trip is 300 km and the car does 15 km per litre, you need 300 / 15 = 20.0 litres.
Common mistake: dividing the wrong way round. per_litre / distance would give a tiny number. Ask: "how many 15s fit in 300?" That is 300 / 15.