Question

Write a program to determine your car’s gas mileage in Python

Write a program to determine your car’s gas mileage in Python

0 0
Add a comment Improve this question Transcribed image text
Answer #1

##Question : Write a program to determine your car’s gas mileage in Python

print("This program calculates car's gas mileage")

#Asking the user to input the distance covered or travelled by the car
distance_travelled = float(input("Enter the distance covered by the car in km "))

#Asking the user to input the gas used by the car
gas_used = float(input("Enter the gas used by the car in atm "))

#Calculating the gas mileage of the car by dividing diatance traveled by the gas used
gas_mileage = distance_travelled / gas_used

#Printing the car's gas mileage
print("Car's gas mileage ", gas_mileage)

OUTPUT

PLEASE PLEASE GIVE A THUMBS UP

COMMENT DOWN FOR ANY QUERY REGARDING THIS QUESTION

Add a comment
Answer #2
 # main.py

def calculate_mileage():
    print("Car Gas Mileage Calculator")
    print("--------------------------")
    
    try:
        miles = float(input("Enter miles driven: "))
        gallons = float(input("Enter gallons of gas used: "))
        
        if gallons <= 0:
            print("Error: Gallons used must be greater than zero.")
            return
        
        mileage = miles / gallons
        print(f"\nYour car's gas mileage is: {mileage:.2f} miles per gallon")
    except ValueError:
        print("Error: Please enter valid numbers for miles and gallons.")

# Run the program
calculate_mileage()



 OUTPUT:- 
 
 Car Gas Mileage Calculator
--------------------------
Enter miles driven: 100
Enter gallons of gas used: 20

Your car's gas mileage is: 5.00 miles per gallon
source: Self
answered by: Harshwardhan kunal
Add a comment
Answer #3
def calculate_mileage():
  """Calculates the car's gas mileage (miles per gallon)."""
  try:
    miles_driven = float(input("Enter the number of miles driven: "))
    gallons_used = float(input("Enter the number of gallons of gas used: "))
    if gallons_used == 0:
      print("Gallons used cannot be zero. Mileage cannot be calculated.")
      return
    mileage = miles_driven / gallons_used
    print(f"Your car's gas mileage is: {mileage:.2f} miles per gallon.")
  except ValueError:
    print("Invalid input. Please enter numeric values.")
# Example usage
calculate_mileage()


answered by: anonymous
Add a comment
Know the answer?
Add Answer to:
Write a program to determine your car’s gas mileage in Python
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT