39 lines
		
	
	
		
			No EOL
		
	
	
		
			1.1 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			39 lines
		
	
	
		
			No EOL
		
	
	
		
			1.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],[290,18],[300,16]]
 | 
						|
 | 
						|
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
 | 
						|
 | 
						|
print("")
 | 
						|
print("       Miles Gallons Miles/Gallon")
 | 
						|
 | 
						|
for fillup in fillupList:
 | 
						|
#    print("This fillup data ",fillup)
 | 
						|
    miles = fillup[0]
 | 
						|
    gallons = fillup[1]
 | 
						|
 | 
						|
    milesPerGallon = calculateMilesPerGallon(miles,gallons)
 | 
						|
    print(f"{miles:10} {gallons:6} {milesPerGallon:10.1f}") |