196k views
2 votes
{Chance to earn 100 points!!!}

Create a function (using Python) to determine how much it'll cost for gas to drive to a destination with the following arguments:
-miles per gallon
-number of miles
-cost per gallon
Trip #1: You are driving from Winchester, VA to Pittsburgh, PA, which is about 190 miles. The cost of gas in PA is about $3.62/gallon. Your car gets about 25 mpg. Find the cost you'll pay for gas.
Trip #2: You are driving from Winchester, VA to Washington, DC which is about 75 miles. The cost of gas in Virginia is about $3.14/gallon. You decide to take your friend's car, which gets about 28 mpg. Find the cost you'll pay for gas.
Trip #3: You are driving from Winchester, VA to Myrtle Beach, SC which is about 460 miles. The cost of gas in North Carolina is about $3.12/gallon. You decide to take your mom's old van, which only gets about 20mpg. Find the cost you'll pay for gas.

User Guicey
by
7.7k points

2 Answers

7 votes

Answer:

def gasCost(MpG, numMiles, CpG):

# Figuring out number/gallons

totGallons = numMiles / MpG

# Finding cost

totCost = totGallons * CpG

# You can round up using the math module, but as you have not mentioned modules, I'll leave it in an unrounded state

return totCost

Step-by-step explanation:

Fairly simple stuff, though I chose not to round up, as that would need another module. You can use the function by putting it in a print statement and seeing what it'll return, E.G:

print(gasCost(25, 190, 3.52)

User Arpeggio
by
7.9k points
6 votes

Here’s a Python function that calculates the cost of gas for a trip:

def calculate_gas_cost(mpg, miles, cost_per_gallon):

gallons = miles / mpg

cost = gallons * cost_per_gallon

return cost

For Trip #1, you would call the function like this:

calculate_gas_cost(25, 190, 3.62)

The cost of gas for this trip would be $27.56.

For Trip #2, you would call the function like this:

calculate_gas_cost(28, 75, 3.14)

The cost of gas for this trip would be $8.13.

For Trip #3, you would call the function like this:

calculate_gas_cost(20, 460, 3.12)

The cost of gas for this trip would be $86.88.

I hope this helps! Let me know if you have any other questions.

User Sudhir Sinha
by
7.6k points

No related questions found

Welcome to QAmmunity.org, where you can ask questions and receive answers from other members of our community.