132,506 views
29 votes
29 votes
Driving is expensive. Write a program (in Python) 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))

User Aladine
by
3.0k points

1 Answer

23 votes
23 votes

x=float(input("How many miles can your vehicle travel on 1 gallon of gasoline?: "))

y=float(input("How much does 1 gallon of gasoline cost?: "))

print('20 miles: {:.2f}$\\75 miles: {:.2f}$\\500 miles: {:.2f}$'.format((20/x)*y, (75/x)*y, (500/x)*y))

User SGhaleb
by
3.3k points