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 20 miles, 75 miles, and 500 miles.

Output each floating-point value with two digits after the decimal point, which can be achieved as follows:
print('{:.2f} {:.2f} {:.2f}'.format(your_value1, your_value2, your_value3))

Ex: If the input is:

20.0
3.1599
Then the output is:

3.16 11.85 79.00

Respuesta :

Answer:

# Program in Python

# Take miles/gallon as input from user

miles_per_gallon = float(input('Please enter cars miles/gallon: '))

# Take dollars/gallon as input from user

dollars_per_gallon = float(input('Please enter gas dollars/gallon: '))

# Formula for calculating gas cost

dollars_per_mile = dollars_per_gallon/miles_per_gallon

# Gas cost for 20 miles

your_value1 = 20 * dollars_per_mile

# Gas cost for 75 miles

your_value2 = 75 * dollars_per_mile

# Gas cost for 500 miles

your_value3 = 500 * dollars_per_mile

# Display the results as output

print('{:.2f} {:.2f} {:.2f}'.format(your_value1, your_value2, your_value3))

Explanation:

First of all take miles/gallon and dollars/gallon as input from user  and store them in their respective variables. Then calculate the gas cost by dividing dollars/gallon with miles/gallon. Multiply this calculated cost with 20, 75 and 500 respectively. Finally display the results by using print statement.

Output:

Please enter cars miles/gallon: 20.0

Please enter gas dollars/gallon: 3.1599

3.16 11.85 79.00