#509 · Python Basics · Beginner · 25 XP · about 3 min
A family is planning a road trip. Their car goes miles_per_gallon miles on one gallon of gas, and gas costs price_per_gallon dollars a gallon. Write fuel_cost(miles, miles_per_gallon, price_per_gallon) that returns what the gas for a trip of miles miles costs, rounded to 2 decimal places with round(value, 2).
fuel_cost(300, 30, 3.5) # returns 35.0 fuel_cost(120, 40, 4.2) # returns 12.6 fuel_cost(250, 25, 3.99) # returns 39.9
In the first example the trip needs 300 / 30 = 10 gallons, and 10 gallons at 3.5 dollars cost 35 dollars.
miles_per_gallon is always more than 0.
First solves only, ranked by stars and then time.