'How to fix this code to calculate correct costs in Python?

Driving is expensive. Write a program with a car's miles/gallon and gas dollars/gallon (both floats) as input, and output the gas cost for 10 miles, 50 miles, and 400 miles.

Ex: If the input is:

20.0
3.1599

Then the output is:

1.579950 7.899750 63.198000

Note: To solve the problem, calculate first the gas cost for driving a mile. Then use this result to compute the gas cost for 10 miles, 50 miles, and 400 miles.

Note: Real per-mile cost would also include maintenance and depreciation.

My code:

gas_cost = float(input())
per_mile = 1 / gas_cost
ten_mile = per_mile * 10
fifty_mile = per_mile * 50
fourh_mile = per_mile * 400
print( ten_mile , fifty_mile , fourh_mile )

Output differs. See highlights below. Input

20.0
3.1599

Your output

0.5 2.5 20.0

Expected output

1.57995 7.89975 63.198


Solution 1:[1]

On a different question similar to this one, but using functions within python I used this:

def driving_cost(driven_miles, miles_per_gallon, dollars_per_gallon):
    dm = miles_per_gallon / dollars_per_gallon
    gc = driven_miles / dm
    return gc

if __name__ == '__main__':
    gas_efficiency = float(input())
    gas_cost = float(input())
    print('{:.2f}'.format(driving_cost(10, gas_efficiency, gas_cost)))
    print('{:.2f}'.format(driving_cost(50, gas_efficiency, gas_cost)))
    print('{:.2f}'.format(driving_cost(400, gas_efficiency, gas_cost)))

Solution 2:[2]

You are not taking the two inputs you mentioned in your problem. That's the first key of your problem. You have given two inputs: engine efficiency (miles/gallon) and gas cost (dollars/gallon). So, first you need to find how much gallons of gas you need for 1 mile. Then you need to multiply this with the cost of gas per gallon. It will give you how much dollar you required for 1 mile. Then you can multiply it with 10, 50, and 400 to calculate results for 10 miles, 50 miles, and 400 miles.

The following code works fine for your sample input:

gas_efficiency = float(input())
gas_cost = float(input())
per_mile = gas_cost / gas_efficiency
ten_mile = per_mile * 10
fifty_mile = per_mile * 50
fourh_mile = per_mile * 400
print( ten_mile , fifty_mile , fourh_mile)

Solution 3:[3]

def driving_cost(driven_miles, miles_per_gallon, dollars_per_gallon):
    driving_cost = dollars_per_gallon * (driven_miles / miles_per_gallon)
    return driving_cost

if __name__ == '__main__':
    miles_per_gallon = float(input())
    dollars_per_gallon = float(input())
    driven_miles = 10
    print('%0.2f' % driving_cost(driven_miles, miles_per_gallon, dollars_per_gallon))
    driven_miles = 50
    print('%0.2f' % driving_cost(driven_miles, miles_per_gallon, dollars_per_gallon))
    driven_miles = 400
    print('%0.2f' % driving_cost(driven_miles, miles_per_gallon, dollars_per_gallon))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Aaron Montgomery
Solution 2 biqarboy
Solution 3 ALee