From 5b643a8abfc23949692f59d8b136090e60258347 Mon Sep 17 00:00:00 2001 From: Alex Date: Tue, 25 Mar 2025 21:51:46 -0700 Subject: [PATCH] Add 2.py --- 2.py | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 2.py diff --git a/2.py b/2.py new file mode 100644 index 0000000..df98bf6 --- /dev/null +++ b/2.py @@ -0,0 +1,96 @@ +import math + + +def volume(): + # Ask for the shape of the object + choice = input( + "Enter shape of object. 1 for Cylinder, 2 for Sphere, 3 for Cone, or 4 for Rectangular Box: ") + + # Check if the input is numeric and within range + if not choice.isdigit(): + print( + "Input not recognized. Please choose a shape using 1, 2, 3, or 4!") + return + else: + choice = int(choice) + + if choice not in [1, 2, 3, 4]: + print( + "TInput not recognized. Please choose a shape using 1, 2, 3, or 4!") + return + + # Cylinder + if choice == 1: + radius = input("Enter the cylinder radius: ") + height = input("Enter the cylinder height: ") + + # Validate radius and height + # Remove any '.' from input to string to check if valid numeral + testRadius = radius.replace('.', '') + testHeight = height.replace('.', '') + if not testRadius.isdigit() or not testHeight.isdigit(): + print("Please enter a valid number") + return + else: + radius = float(radius) + height = float(height) + + volume = math.pi * radius ** 2 * height + print("Volume is: ", volume, "units") + + elif choice == 2: # Sphere + radius = input("Enter radius of the sphere: ") + + # Validate radius inputs + if not radius.replace('.', '').isdigit(): + print("Please enter a valid number for radius.") + return + else: + radius = float(radius) + + + volume = (4 / 3) * math.pi * radius ** 3 + print("Volume: ", volume, "units") + + elif choice == 3: # Cone + radius = input("Enter radius of the cone: ") + height = input("Enter height of the cone: ") + + # Validate radius and height + testRadius = radius.replace('.', '') + testHeight = height.replace('.', '') + if not testRadius.isdigit() or not testHeight.isdigit(): + print("Please enter a valid number") + return + else: + radius = float(radius) + height = float(height) + + volume = (math.pi * radius ** 2 * height) / 3 + print("Volume: ", volume, "units") + + elif choice == 4: # Rectangular Box + width = input("Enter the width of the rectangular box: ") + height = input("Enter the height of the rectangular box: ") + length = input("Enter the length of the rectangular box: ") + + # Validate width, height, and length + testWidth = width.replace('.', '') + testHeight = height.replace('.', '') + testLength = length.replace('.','') + + if not testWidth.isdigit() or not testHeight.isdigit() or not testLength.isdigit(): + print("Please enter a valid number") + return + + else: + width = float(width) + height = float(height) + length = float(length) + + volume = width * height * length + print("Volume: ", volume, "units") + + +# Call the volume function +volume()