Lecture 10-28 code

This commit is contained in:
Edward Bigos 2025-10-28 09:00:49 -04:00
parent 97b81f2b77
commit 12e989631c
11 changed files with 288 additions and 0 deletions

View file

@ -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}")

View file

@ -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}")

View file

@ -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}")

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
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}")

View file

@ -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}")

View file

@ -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}")

View file

@ -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}")

View file

@ -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}")

View file

@ -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}")

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
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}")

View file

@ -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}")