From 12e989631c5ce41c71c7eed71af7253b4682fa3e Mon Sep 17 00:00:00 2001 From: Edward Bigos Date: Tue, 28 Oct 2025 09:00:49 -0400 Subject: [PATCH] Lecture 10-28 code --- .../function1/rectangle-area06.py | 29 +++++++++++++++ .../list01/milesPerGallon04.py | 35 +++++++++++++++++++ .../lecture20251028/try1/milesPerGallon01.py | 27 ++++++++++++++ .../lecture20251028/try1/milesPerGallon02.py | 32 +++++++++++++++++ .../lecture20251028/try1/rectangle-area01.py | 15 ++++++++ .../lecture20251028/try1/rectangle-area02.py | 20 +++++++++++ .../lecture20251028/try1/rectangle-area03.py | 21 +++++++++++ .../lecture20251028/try1/rectangle-area04.py | 22 ++++++++++++ .../lecture20251028/try1/rectangle-area05.py | 28 +++++++++++++++ .../lecture20251028/try2/milesPerGallon03.py | 32 +++++++++++++++++ .../lecture20251028/try2/rectangle-area07.py | 27 ++++++++++++++ 11 files changed, 288 insertions(+) create mode 100644 bigos/lecture20251028/function1/rectangle-area06.py create mode 100644 bigos/lecture20251028/list01/milesPerGallon04.py create mode 100644 bigos/lecture20251028/try1/milesPerGallon01.py create mode 100644 bigos/lecture20251028/try1/milesPerGallon02.py create mode 100644 bigos/lecture20251028/try1/rectangle-area01.py create mode 100644 bigos/lecture20251028/try1/rectangle-area02.py create mode 100644 bigos/lecture20251028/try1/rectangle-area03.py create mode 100644 bigos/lecture20251028/try1/rectangle-area04.py create mode 100644 bigos/lecture20251028/try1/rectangle-area05.py create mode 100644 bigos/lecture20251028/try2/milesPerGallon03.py create mode 100644 bigos/lecture20251028/try2/rectangle-area07.py diff --git a/bigos/lecture20251028/function1/rectangle-area06.py b/bigos/lecture20251028/function1/rectangle-area06.py new file mode 100644 index 0000000..d9203b2 --- /dev/null +++ b/bigos/lecture20251028/function1/rectangle-area06.py @@ -0,0 +1,29 @@ +#!/usr/bin/python3 +import sys + +# Program: reactangle-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 + +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) + +area = length * width + +print(f"The area of the rectangle is {area:.1f}") diff --git a/bigos/lecture20251028/list01/milesPerGallon04.py b/bigos/lecture20251028/list01/milesPerGallon04.py new file mode 100644 index 0000000..8d61798 --- /dev/null +++ b/bigos/lecture20251028/list01/milesPerGallon04.py @@ -0,0 +1,35 @@ +#!/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 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 + + + +miles = getFloatValue("Enter a number of miles traveled") +gallons = getFloatValue("Enter the number of gallons on the last fill-up") + +milesPerGallon = calculateMilesPerGallon(miles,gallons) + +print(f"The miles per gallon is {milesPerGallon:.1f}") \ No newline at end of file diff --git a/bigos/lecture20251028/try1/milesPerGallon01.py b/bigos/lecture20251028/try1/milesPerGallon01.py new file mode 100644 index 0000000..7f68d33 --- /dev/null +++ b/bigos/lecture20251028/try1/milesPerGallon01.py @@ -0,0 +1,27 @@ +#!/usr/bin/python3 +import sys + +# Program: milesPerGallon01.py +# Objective: Calculate the milage for a vehicle using the fill-up data. +# Revisions +# None +# Edward Bigos + + +try: + miles = float(input("Enter a number of miles traveled: ")) +except ValueError as error: + print("Error: ",error) + print("You have entered an invalid value. The input must be a number.") + sys.exit(-1) + +try: + gallons = float(input("Enter the number of gallons on the last fill-up: ")) +except ValueError as error: + print("Error: ",error) + print("You have entered an invalid value. The input must be a number.") + sys.exit(-1) + +milesPerGallon = miles/gallons + +print(f"The miles per gallon is {milesPerGallon:.1f}") \ No newline at end of file diff --git a/bigos/lecture20251028/try1/milesPerGallon02.py b/bigos/lecture20251028/try1/milesPerGallon02.py new file mode 100644 index 0000000..e54a332 --- /dev/null +++ b/bigos/lecture20251028/try1/milesPerGallon02.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 + + +try: + miles = float(input("Enter a number of miles traveled: ")) +except ValueError as error: + print("Error: ",error) + print("You have entered an invalid value. The input must be a number.") + sys.exit(-1) + +try: + gallons = float(input("Enter the number of gallons on the last fill-up: ")) +except ValueError as error: + print("Error: ",error) + print("You have entered an invalid value. The input must be a number.") + sys.exit(-1) + +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/try1/rectangle-area01.py b/bigos/lecture20251028/try1/rectangle-area01.py new file mode 100644 index 0000000..bb2d595 --- /dev/null +++ b/bigos/lecture20251028/try1/rectangle-area01.py @@ -0,0 +1,15 @@ +#!/usr/bin/python3 + +# Program: reactangle-area01.py +# Objective: Calculate the area of a rectangle. No error checking. +# Edward Bigos + +length = float(input("Enter a value for length ")) + + +width = input("Enter a value for width ") +width = float(width) + +area = length * width + +print(f"The area of the rectangle is {area:.1f}") diff --git a/bigos/lecture20251028/try1/rectangle-area02.py b/bigos/lecture20251028/try1/rectangle-area02.py new file mode 100644 index 0000000..f993eb2 --- /dev/null +++ b/bigos/lecture20251028/try1/rectangle-area02.py @@ -0,0 +1,20 @@ +#!/usr/bin/python3 +import sys + +# Program: reactangle-area02.py +# Objective: Calculate the area of a rectangle. Check for numeric error on length. +# No error checking on width yet. +# Edward Bigos + +try: + length = float(input("Enter a value for length ")) +except: + 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) + +area = length * width + +print(f"The area of the rectangle is {area:.1f}") diff --git a/bigos/lecture20251028/try1/rectangle-area03.py b/bigos/lecture20251028/try1/rectangle-area03.py new file mode 100644 index 0000000..2ba9a67 --- /dev/null +++ b/bigos/lecture20251028/try1/rectangle-area03.py @@ -0,0 +1,21 @@ +#!/usr/bin/python3 +import sys + +# Program: reactangle-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. +# Edward Bigos + +try: + length = float(input("Enter a value for length ")) +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) + +area = length * width + +print(f"The area of the rectangle is {area:.1f}") diff --git a/bigos/lecture20251028/try1/rectangle-area04.py b/bigos/lecture20251028/try1/rectangle-area04.py new file mode 100644 index 0000000..4e807ee --- /dev/null +++ b/bigos/lecture20251028/try1/rectangle-area04.py @@ -0,0 +1,22 @@ +#!/usr/bin/python3 +import sys + +# Program: reactangle-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. +# 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) + +width = input("Enter a value for width ") +width = float(width) + +area = length * width + +print(f"The area of the rectangle is {area:.1f}") diff --git a/bigos/lecture20251028/try1/rectangle-area05.py b/bigos/lecture20251028/try1/rectangle-area05.py new file mode 100644 index 0000000..9744f54 --- /dev/null +++ b/bigos/lecture20251028/try1/rectangle-area05.py @@ -0,0 +1,28 @@ +#!/usr/bin/python3 +import sys + +# Program: reactangle-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. +# 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) + +area = length * width + +print(f"The area of the rectangle is {area:.1f}") diff --git a/bigos/lecture20251028/try2/milesPerGallon03.py b/bigos/lecture20251028/try2/milesPerGallon03.py new file mode 100644 index 0000000..e54a332 --- /dev/null +++ b/bigos/lecture20251028/try2/milesPerGallon03.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 + + +try: + miles = float(input("Enter a number of miles traveled: ")) +except ValueError as error: + print("Error: ",error) + print("You have entered an invalid value. The input must be a number.") + sys.exit(-1) + +try: + gallons = float(input("Enter the number of gallons on the last fill-up: ")) +except ValueError as error: + print("Error: ",error) + print("You have entered an invalid value. The input must be a number.") + sys.exit(-1) + +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/try2/rectangle-area07.py b/bigos/lecture20251028/try2/rectangle-area07.py new file mode 100644 index 0000000..e58a936 --- /dev/null +++ b/bigos/lecture20251028/try2/rectangle-area07.py @@ -0,0 +1,27 @@ +#!/usr/bin/python3 +import sys + +# Program: reactangle-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 the width") + +area = length * width + +print(f"The area of the rectangle is {area:.1f}")