2025faCSE160/bigos/lecture20251028/list01/milesPerGallon04.py

36 lines
No EOL
1 KiB
Python

#!/usr/bin/python3
import sys
# Program: milesPerGallon04.py
# Objective: Calculate the milage for a vehicle using the fill-up data.
# Revisions
# Check for division by zero error
# Edward Bigos
fillupList = [[200,10],[400,19], [190,9.8]]
def calculateMilesPerGallon(miles,gallons):
try:
milesPerGallon = miles / gallons
except ZeroDivisionError as error:
print("Error: ", error)
print("You have entered an invalid value for the gallons. The input must greater than zero.")
sys.exit(-1)
return milesPerGallon
def getFloatValue(message):
try:
fValue = float(input(message + ": "))
except ValueError as error:
print("Error: ", error)
print("You have entered an invalid value. The input must be a number.")
sys.exit(-1)
return fValue
for fillup in fillupList:
# print("This fillup data ",fillup)
miles = fillup[0]
gallons = fillup[1]
milesPerGallon = calculateMilesPerGallon(miles,gallons)
print(f"The miles per gallon is {milesPerGallon:.1f}")