24.3k views
4 votes
Al’s Last Chance Gas Station sits on route 190 on the edge of Death Valley. Write a program using if or Switch to help drivers decide if they need gas. The code should ask for the capacity of the gas tank in gallons, the indication of the gas gauge in percent, and the miles per gallon that the car usually gets. The program then writes out "Get Gas" or "Safe to Proceed" based on whether the car can cross the 200 miles with the gas remaining in the tank. Include the estimated distance in miles the car should be able to drive before running out of gas.

User Evanb
by
8.2k points

1 Answer

0 votes

Final answer:

Here's a sample code in Python:

```python

# Prompt the user for input

capacity = float(input("Enter the capacity of your gas tank in gallons: "))

gauge_indication = float(input("Enter the gas gauge indication in percent: "))

miles_per_gallon = float(input("Enter the miles per gallon your car usually gets: "))

# Calculate the estimated distance

estimated_distance = (capacity * gauge_indication / 100) * miles_per_gallon

# Check if gas is needed

if estimated_distance < 200:

print("Get Gas. Estimated distance: ", estimated_distance, " miles")

else:

print("Safe to Proceed. Estimated distance: ", estimated_distance, " miles")

```

Step-by-step explanation:

Here's an example program using if statements to help drivers decide if they need gas:

1. Start by prompting the user to enter the capacity of their gas tank in gallons, the gas gauge indication in percent, and the miles per gallon their car usually gets.

2. Store the input values in appropriate variables.

3. Calculate the estimated distance the car can travel before running out of gas by multiplying the capacity of the gas tank by the gas gauge indication (converted to a decimal) and then dividing it by the miles per gallon.

4. Use an if statement to check if the estimated distance is less than 200 miles.

  • - If the estimated distance is less than 200 miles, output "Get Gas" and the estimated distance.
  • - If the estimated distance is greater than or equal to 200 miles, output "Safe to Proceed" and the estimated distance.

By running this program, the user will be able to determine whether they need to get gas or if it's safe to proceed based on their gas tank capacity, gas gauge indication, and miles per gallon their car usually gets. The program will also display the estimated distance the car should be able to drive before running out of gas.

User Gifty
by
7.2k points