Explore

Python Programs

Miles Per Gallon Python Program

Miles Per Gallon Python Program

Want to know how to create miles per gallon python program, then you are at the right place today in this tutorial. I will show you how to create a python program to calculate miles per gallon, so follow this tutorial till the end.

Miles Per Gallon is how many gallons of gas is used in certain miles. It is a very easy and simple program. I will show you everything on how to create this program, so let’s see how this program will work.

So we will write a program that asks the user for the number of miles driven and the gallons of gas used in python then we will divide them and it will give us the miles per gallon.

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. Now to run this program 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.

To run this python program, follow the below steps:

  • Create a new folder for this python project.
  • Open it in a code editor of your choice.
  • Create a python file with an ending .py extension.
  • Copy the above code and paste it in your file.
  • Open a terminal or CMD at folder location
  • And type this command python filename.py (Name of your file).

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

Don’t want to create all the files and folders then you can run this program now using this online python compiler it is fast and easy.

Summary

This was the python program to calculate 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 🙂