Back

Code Challenge: Fuel Calculator

Difficulty  

divider

Objective

Work out how much gas a trip needs and what it will cost, from the distance, the vehicle's gas mileage, and the price of gas.

Skills to Practice

  • Prompting a user for input
  • Saving user input to variables
  • Converting input to the correct data type
  • Performing arithmetic calculations
  • Formatting custom output using variables
  • Rounding decimal numbers

Tasks

  • Create a new Python program named cc-fuel-calculator.
  • Prompt the user for three values — the distance, the vehicle's gas mileage in mpg, and the price of gas per gallon.
  • Calculate the total gallons of gas needed.
  • Round the gallons to one decimal place before you calculate the cost — that is how the sample output arrives at $12.98.
  • Calculate the total cost of that gas.
  • Display both results in a clear, formatted way.

Sample Output

Sample Output
--- Fuel Calculator ---
How many miles are you traveling? 150 [Enter]
What is your vehicle's gas mileage (mpg)? 34 [Enter]
What is the current price of gas per gallon? $2.95 [Enter]
For your trip, you will need 4.4 gallons of gas costing $12.98 total.

Commence Challenge