Baloo
Baloo

Reputation: 7

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

Upvotes: -2

Views: 23421

Answers (4)

Lord DAGF
Lord DAGF

Reputation: 1

I Had a similar question to this and this worked for me

miles = float(input())
gas = float(input())
mpg = gas/miles

gas_cost1= (mpg)* 20
gas_cost2= (mpg)* 75
gas_cost3= (mpg)* 500

print(f'{gas_cost1:.2f} {gas_cost2:.2f} {gas_cost3:.2f}')

Upvotes: 0

ALee
ALee

Reputation: 9

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))

Upvotes: 0

Aaron Montgomery
Aaron Montgomery

Reputation: 11

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)))

Upvotes: 1

biqarboy
biqarboy

Reputation: 852

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)

Upvotes: 1

Related Questions