Miles-per-Gallon

A car’s miles-per-gallon (MPG) can be calculated with the following formula:

MPG = Miles driven / Gallons of gas used

Write a program that asks the user for the number of miles driven and the gallons of gas used. It should calculate the car’s MPG and display the result.

Solution

#Get the number of miles driven

milesDriven = float(input(“Enter the number of miles driven : “))

#Get the gallons of gas used

gasUsed = int(input(“Enter the gallons of gas used : “))

#calculate the MPG

mpg = milesDriven * gasUsed

 

#display the car’s MPG

print(“car’s MPG :”,format(mpg , ‘,.2f’))