From c565f0446113685156a6bc52dc7adfad61a80e64 Mon Sep 17 00:00:00 2001 From: Edward Bigos Date: Tue, 28 Oct 2025 10:32:13 -0400 Subject: [PATCH] After the 10/28 lecture --- .../function1/milesPerGallon04a.py | 32 +++++++++++++++ .../function1/rectangle-area06-function.py | 26 +++++++++++++ .../function1/rectangle-area06.py | 4 +- .../function1/rectangle-volume2.py | 29 ++++++++++++++ .../list01/milesPerGallon04.py | 1 + .../list01/milesPerGallon05.py | 37 ++++++++++++++++++ .../list01/milesPerGallon06.py | 39 +++++++++++++++++++ .../lecture20251028/try1/milesPerGallon01.py | 7 +++- .../lecture20251028/try1/rectangle-area01.py | 2 +- .../lecture20251028/try1/rectangle-area02.py | 2 +- .../lecture20251028/try1/rectangle-area03.py | 12 ++++-- .../lecture20251028/try1/rectangle-area04.py | 12 ++++-- .../lecture20251028/try1/rectangle-area05.py | 2 +- .../lecture20251028/try1/rectangle-volume.py | 36 +++++++++++++++++ .../lecture20251028/try2/rectangle-area07.py | 2 +- 15 files changed, 228 insertions(+), 15 deletions(-) create mode 100644 bigos/lecture20251028/function1/milesPerGallon04a.py create mode 100644 bigos/lecture20251028/function1/rectangle-area06-function.py create mode 100644 bigos/lecture20251028/function1/rectangle-volume2.py create mode 100644 bigos/lecture20251028/list01/milesPerGallon05.py create mode 100644 bigos/lecture20251028/list01/milesPerGallon06.py create mode 100644 bigos/lecture20251028/try1/rectangle-volume.py diff --git a/bigos/lecture20251028/function1/milesPerGallon04a.py b/bigos/lecture20251028/function1/milesPerGallon04a.py new file mode 100644 index 0000000..f86d60f --- /dev/null +++ b/bigos/lecture20251028/function1/milesPerGallon04a.py @@ -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}") \ No newline at end of file diff --git a/bigos/lecture20251028/function1/rectangle-area06-function.py b/bigos/lecture20251028/function1/rectangle-area06-function.py new file mode 100644 index 0000000..fbb8705 --- /dev/null +++ b/bigos/lecture20251028/function1/rectangle-area06-function.py @@ -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}") diff --git a/bigos/lecture20251028/function1/rectangle-area06.py b/bigos/lecture20251028/function1/rectangle-area06.py index ec46838..81a7205 100644 --- a/bigos/lecture20251028/function1/rectangle-area06.py +++ b/bigos/lecture20251028/function1/rectangle-area06.py @@ -1,8 +1,8 @@ #!/usr/bin/python3 import sys -# Program: milesPerGallon04.py -# Objective: Calculate the milage at fill-up. +# 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. diff --git a/bigos/lecture20251028/function1/rectangle-volume2.py b/bigos/lecture20251028/function1/rectangle-volume2.py new file mode 100644 index 0000000..703092e --- /dev/null +++ b/bigos/lecture20251028/function1/rectangle-volume2.py @@ -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}") diff --git a/bigos/lecture20251028/list01/milesPerGallon04.py b/bigos/lecture20251028/list01/milesPerGallon04.py index d92e60a..ca6ec88 100644 --- a/bigos/lecture20251028/list01/milesPerGallon04.py +++ b/bigos/lecture20251028/list01/milesPerGallon04.py @@ -28,6 +28,7 @@ def getFloatValue(message): return fValue for fillup in fillupList: +# print("This fillup data ",fillup) miles = fillup[0] gallons = fillup[1] diff --git a/bigos/lecture20251028/list01/milesPerGallon05.py b/bigos/lecture20251028/list01/milesPerGallon05.py new file mode 100644 index 0000000..5d64942 --- /dev/null +++ b/bigos/lecture20251028/list01/milesPerGallon05.py @@ -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") \ No newline at end of file diff --git a/bigos/lecture20251028/list01/milesPerGallon06.py b/bigos/lecture20251028/list01/milesPerGallon06.py new file mode 100644 index 0000000..77d1f88 --- /dev/null +++ b/bigos/lecture20251028/list01/milesPerGallon06.py @@ -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}") \ No newline at end of file diff --git a/bigos/lecture20251028/try1/milesPerGallon01.py b/bigos/lecture20251028/try1/milesPerGallon01.py index 7f68d33..441d660 100644 --- a/bigos/lecture20251028/try1/milesPerGallon01.py +++ b/bigos/lecture20251028/try1/milesPerGallon01.py @@ -22,6 +22,11 @@ except ValueError as error: print("You have entered an invalid value. The input must be a number.") 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}") \ No newline at end of file diff --git a/bigos/lecture20251028/try1/rectangle-area01.py b/bigos/lecture20251028/try1/rectangle-area01.py index bb2d595..ebd945b 100644 --- a/bigos/lecture20251028/try1/rectangle-area01.py +++ b/bigos/lecture20251028/try1/rectangle-area01.py @@ -1,6 +1,6 @@ #!/usr/bin/python3 -# Program: reactangle-area01.py +# Program: rectangle-area01.py # Objective: Calculate the area of a rectangle. No error checking. # Edward Bigos diff --git a/bigos/lecture20251028/try1/rectangle-area02.py b/bigos/lecture20251028/try1/rectangle-area02.py index f993eb2..05019d9 100644 --- a/bigos/lecture20251028/try1/rectangle-area02.py +++ b/bigos/lecture20251028/try1/rectangle-area02.py @@ -1,7 +1,7 @@ #!/usr/bin/python3 import sys -# Program: reactangle-area02.py +# Program: rectangle-area02.py # Objective: Calculate the area of a rectangle. Check for numeric error on length. # No error checking on width yet. # Edward Bigos diff --git a/bigos/lecture20251028/try1/rectangle-area03.py b/bigos/lecture20251028/try1/rectangle-area03.py index 2ba9a67..98b4a63 100644 --- a/bigos/lecture20251028/try1/rectangle-area03.py +++ b/bigos/lecture20251028/try1/rectangle-area03.py @@ -1,10 +1,10 @@ #!/usr/bin/python3 import sys -# Program: reactangle-area03.py +# Program: rectangle-area03.py # Objective: Calculate the area of a rectangle. Check for numeric error on length. # Specify the type of the error as ValueError -# No error checking on width yet. +# Added error checking for width. # Edward Bigos try: @@ -13,8 +13,12 @@ except ValueError: print("You have entered an invalid value. The input must be a number.") sys.exit(-1) -width = input("Enter a value for width ") -width = float(width) +try: + 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 diff --git a/bigos/lecture20251028/try1/rectangle-area04.py b/bigos/lecture20251028/try1/rectangle-area04.py index 4e807ee..7c2e6e5 100644 --- a/bigos/lecture20251028/try1/rectangle-area04.py +++ b/bigos/lecture20251028/try1/rectangle-area04.py @@ -1,10 +1,10 @@ #!/usr/bin/python3 import sys -# Program: reactangle-area04.py +# Program: rectangle-area04.py # 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. -# No error checking on width yet. +# Added error checking for width. # Edward Bigos try: @@ -14,8 +14,12 @@ except ValueError as error: print("You have entered an invalid value. The input must be a number.") sys.exit(-1) -width = input("Enter a value for width ") -width = float(width) +try: + 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 diff --git a/bigos/lecture20251028/try1/rectangle-area05.py b/bigos/lecture20251028/try1/rectangle-area05.py index 9744f54..3904968 100644 --- a/bigos/lecture20251028/try1/rectangle-area05.py +++ b/bigos/lecture20251028/try1/rectangle-area05.py @@ -1,7 +1,7 @@ #!/usr/bin/python3 import sys -# Program: reactangle-area05.py +# Program: rectangle-area05.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. diff --git a/bigos/lecture20251028/try1/rectangle-volume.py b/bigos/lecture20251028/try1/rectangle-volume.py new file mode 100644 index 0000000..0bcf3a4 --- /dev/null +++ b/bigos/lecture20251028/try1/rectangle-volume.py @@ -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}") diff --git a/bigos/lecture20251028/try2/rectangle-area07.py b/bigos/lecture20251028/try2/rectangle-area07.py index e58a936..6b40142 100644 --- a/bigos/lecture20251028/try2/rectangle-area07.py +++ b/bigos/lecture20251028/try2/rectangle-area07.py @@ -1,7 +1,7 @@ #!/usr/bin/python3 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. # Revisions # Specify the type of the error as ValueError and print the error type.