Pythondex
Python Programs

Miles Per Gallon Python Program

Last updated June 2, 2023 by Jarvis Silva

In this tutorial we will see how to create miles per gallon python program, Miles Per Gallon is how many gallons of gas is used in certain miles.

So to create this program we first need to ask the user for the number of miles driven and the gallons of gas used then divide them and it will give us the miles per gallon so let’s code it in python.

Miles Per Gallon Python Program


# Get miles driven from the user
miles_driven = float(input("Enter miles driven: "))
 
# Get gallons used from the user
gallons_used = float(input("Enter gallons used: "))
 
miles_per_gallon = miles_driven / gallons_used

print("Miles per gallon:", miles_per_gallon)

Above is the python program to calculate miles per gallon, as you can see it is a very small program, It first asks the user how many miles driven then how many gallons used and then it divides them both and finally it prints the result

You can run this program on your computer you need to have python installed on your computer, If you don’t have then follow this guide: Install and setup python on your computer or you can use this online python compiler

After running this program you will asked to enter the miles driven and gallons used and then it will print the miles per gallon, Below is an example output.


Enter miles driven: 12
Enter gallons used: 2

Miles per gallon: 6.0

As you can see we successfully calculated miles per gallon, I hope you found this tutorial helpful and useful, Do share this tutorial with your friends who might be interested in this program.

Here are some more python drawing tutorials for you:

I hope you found what you were looking for from this tutorial, and if you want more python programs and tutorials like this, do join our Telegram channel for future updates.

Thanks for reading, have a nice day 🙂