After the 10/28 lecture

This commit is contained in:
Edward Bigos 2025-10-28 10:32:13 -04:00
parent 3fbf3eb87c
commit c565f04461
15 changed files with 228 additions and 15 deletions

View file

@ -0,0 +1,32 @@
#!/usr/bin/python3
import sys
# Program: milesPerGallon01.py
# Objective: Calculate the milage for a vehicle using the fill-up data.
# Revisions
# Check for division by zero error
# Edward Bigos
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
miles = getFloatValue("Enter a number of miles traveled: ")
gallons = getFloatValue("Enter the number of gallons on the last fill-up: ")
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)
print(f"The miles per gallon is {milesPerGallon:.1f}")

View file

@ -0,0 +1,26 @@
#!/usr/bin/python3
import sys
# Program: rectangle-area06.py
# Objective: Calculate the area of a rectangle. Check for numeric error on length.
# Revisions
# Specify the type of the error as ValueError and print the error type.
# Added same to width.
# Moved input code to function
# Edward Bigos
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
length = getFloatValue("Enter a value for length")
width = getFloatValue("Enter a value for width ")
area = length * width
print(f"The area of the rectangle is {area:.1f}")

View file

@ -1,8 +1,8 @@
#!/usr/bin/python3 #!/usr/bin/python3
import sys import sys
# Program: milesPerGallon04.py # Program: rectangle-area06.py
# Objective: Calculate the milage at fill-up. # Objective: Calculate the area of a rectangle. Check for numeric error on length.
# Revisions # Revisions
# Specify the type of the error as ValueError and print the error type. # Specify the type of the error as ValueError and print the error type.
# Added same to width. # Added same to width.

View file

@ -0,0 +1,29 @@
#!/usr/bin/python3
import sys
# Program: rectangle-volume.py
# Objective: Calculate the volume of a rectanglar solid. Check for numeric error on length.
# Revisions
# Specify the type of the error as ValueError and print the error type.
# Added same to width.
# Edward Bigos
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
length = getFloatValue("Enter a value for length ")
width = getFloatValue("Enter a value for width ")
height = getFloatValue("Enter a value for height ")
area = length * width * height
print(f"The volume of the rectanglar solid is {area:.1f}")

View file

@ -28,6 +28,7 @@ def getFloatValue(message):
return fValue return fValue
for fillup in fillupList: for fillup in fillupList:
# print("This fillup data ",fillup)
miles = fillup[0] miles = fillup[0]
gallons = fillup[1] gallons = fillup[1]

View file

@ -0,0 +1,37 @@
#!/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
for fillup in fillupList:
# print("This fillup data ",fillup)
miles = fillup[0]
gallons = fillup[1]
milesPerGallon = calculateMilesPerGallon(miles,gallons)
print(f"Miles = {miles}, Gallons = {gallons}")
print(f"The miles per gallon is {milesPerGallon:.1f}\n")

View file

@ -0,0 +1,39 @@
#!/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}")

View file

@ -22,6 +22,11 @@ except ValueError as error:
print("You have entered an invalid value. The input must be a number.") print("You have entered an invalid value. The input must be a number.")
sys.exit(-1) sys.exit(-1)
milesPerGallon = miles/gallons try:
milesPerGallon = miles/gallons
except ZeroDivisionError as error:
print("Error: ", error)
print("The number of gallons must be greater than zero.")
sys.exit(-2)
print(f"The miles per gallon is {milesPerGallon:.1f}") print(f"The miles per gallon is {milesPerGallon:.1f}")

View file

@ -1,6 +1,6 @@
#!/usr/bin/python3 #!/usr/bin/python3
# Program: reactangle-area01.py # Program: rectangle-area01.py
# Objective: Calculate the area of a rectangle. No error checking. # Objective: Calculate the area of a rectangle. No error checking.
# Edward Bigos # Edward Bigos

View file

@ -1,7 +1,7 @@
#!/usr/bin/python3 #!/usr/bin/python3
import sys import sys
# Program: reactangle-area02.py # Program: rectangle-area02.py
# Objective: Calculate the area of a rectangle. Check for numeric error on length. # Objective: Calculate the area of a rectangle. Check for numeric error on length.
# No error checking on width yet. # No error checking on width yet.
# Edward Bigos # Edward Bigos

View file

@ -1,10 +1,10 @@
#!/usr/bin/python3 #!/usr/bin/python3
import sys import sys
# Program: reactangle-area03.py # Program: rectangle-area03.py
# Objective: Calculate the area of a rectangle. Check for numeric error on length. # Objective: Calculate the area of a rectangle. Check for numeric error on length.
# Specify the type of the error as ValueError # Specify the type of the error as ValueError
# No error checking on width yet. # Added error checking for width.
# Edward Bigos # Edward Bigos
try: try:
@ -13,8 +13,12 @@ except ValueError:
print("You have entered an invalid value. The input must be a number.") print("You have entered an invalid value. The input must be a number.")
sys.exit(-1) sys.exit(-1)
width = input("Enter a value for width ") try:
width = float(width) width = input("Enter a value for width ")
width = float(width)
except ValueError:
print("You have entered an invalid value. The input must be a number.")
sys.exit(-1)
area = length * width area = length * width

View file

@ -1,10 +1,10 @@
#!/usr/bin/python3 #!/usr/bin/python3
import sys import sys
# Program: reactangle-area04.py # Program: rectangle-area04.py
# Objective: Calculate the area of a rectangle. Check for numeric error on length. # Objective: Calculate the area of a rectangle. Check for numeric error on length.
# Specify the type of the error as ValueError and print the error type. # Specify the type of the error as ValueError and print the error type.
# No error checking on width yet. # Added error checking for width.
# Edward Bigos # Edward Bigos
try: try:
@ -14,8 +14,12 @@ except ValueError as error:
print("You have entered an invalid value. The input must be a number.") print("You have entered an invalid value. The input must be a number.")
sys.exit(-1) sys.exit(-1)
width = input("Enter a value for width ") try:
width = float(width) width = float(input("Enter a value for width "))
except ValueError:
print("You have entered an invalid value. The input must be a number.")
sys.exit(-1)
area = length * width area = length * width

View file

@ -1,7 +1,7 @@
#!/usr/bin/python3 #!/usr/bin/python3
import sys import sys
# Program: reactangle-area05.py # Program: rectangle-area05.py
# Objective: Calculate the area of a rectangle. Check for numeric error on length. # Objective: Calculate the area of a rectangle. Check for numeric error on length.
# Revisions # Revisions
# Specify the type of the error as ValueError and print the error type. # Specify the type of the error as ValueError and print the error type.

View file

@ -0,0 +1,36 @@
#!/usr/bin/python3
import sys
# Program: rectangle-volume.py
# Objective: Calculate the volume of a rectanglar solid. Check for numeric error on length.
# Revisions
# Specify the type of the error as ValueError and print the error type.
# Added same to width.
# Edward Bigos
try:
length = float(input("Enter a value for length "))
except ValueError as error:
print("Error: ",error)
print("You have entered an invalid value. The input must be a number.")
sys.exit(-1)
try:
width = input("Enter a value for width ")
width = float(width)
except ValueError as error:
print("Error: ",error)
print("You have entered an invalid value. The input must be a number.")
sys.exit(-1)
try:
height = input("Enter a value for height ")
height = float(height)
except ValueError as error:
print("Error: ",error)
print("You have entered an invalid value. The input must be a number.")
sys.exit(-1)
area = length * width * height
print(f"The volume of the rectanglar solid is {area:.1f}")

View file

@ -1,7 +1,7 @@
#!/usr/bin/python3 #!/usr/bin/python3
import sys import sys
# Program: reactangle-area06.py # Program: Based on rectangle-area06a.py
# Objective: Calculate the area of a rectangle. Check for numeric error on length. # Objective: Calculate the area of a rectangle. Check for numeric error on length.
# Revisions # Revisions
# Specify the type of the error as ValueError and print the error type. # Specify the type of the error as ValueError and print the error type.