Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

‭Republic of the Philippines‬

‭BATANGAS STATE UNIVERSITY‬


‭The National Engineering University‬
‭Alangilan Campus‬
‭Golden Country Homes, Alangilan Batangas City, Batangas, Philippines 4200‬
‭Tel Nos.: (+63 43) 425-0139 local 2121 / 2221‬
‭ -mail Address:‬‭coe.alangilan@g.batstate-u.edu.ph‬‭| Website Address:‬‭http://www.batstate-u.edu.ph‬
E

‭College of Engineering – Department of Civil Engineering‬

‭ AME: DESIPEDA, PRINCESS ANNE D.‬


N ‭DATE: April 26, 2024‬
‭SECTION: SE 1201‬

‭ACTIVITY 3: LOOPS‬

‭Resistor Manager and Total Resistance Calculator‬


‭Instructions:‬
‭1.‬ ‭Options:‬
‭Your program must present the following options repeatedly:‬
‭●‬ ‭Add a resistor: Choose this option to add a resistor to the circuit.‬
‭●‬ ‭Calculate total resistance: Choose this option to calculate the total resistance of the circuit.‬
‭●‬ ‭Exit: Choose this option to exit the program.‬
‭1.‬ ‭Adding a Resistor‬‭:‬
‭●‬ ‭Enter the resistance value of the resistor when prompted.‬
‭●‬ ‭The program will validate the input to ensure it consists of valid characters ('0' to '9' and '.').‬
‭●‬ ‭If the resistance value is valid and greater than zero, it will be added to the list of resistors, and a confirmation‬
‭message will be displayed.‬
‭●‬ ‭If the resistance value is not valid or less than or equal to zero, an appropriate error message will be‬
‭displayed.‬
‭2.‬ ‭Calculating Total Resistance:‬
‭●‬ ‭If no resistors have been added yet, the program will display a message indicating that no resistors have been‬
‭added.‬
‭●‬ ‭If resistors are present:‬
‭●‬ ‭The user must be prompted to enter the connection type (series or parallel).‬
‭●‬ ‭For Series Connection:‬
‭●‬ ‭The program will sum up all the resistance values to calculate the total resistance.‬
‭The total resistance value will be displayed with two decimal places.‬
‭●‬ ‭For Parallel Connection:‬
‭●‬ ‭The program will calculate the total resistance using the formula:‬
‭Total Resistance = 1 / (1/R1 + 1/R2 + 1/R3 + ...) (Reciprocal of the sum of reciprocals of‬
‭all resistances)‬
‭●‬ ‭It iterates over each resistance value, calculates its reciprocal, and sums them up.‬
‭●‬ ‭Finally, it takes the reciprocal of the sum to obtain the total resistance of the parallel‬
‭circuit. The total resistance value will be displayed with two decimal places.‬
‭3.‬ ‭Exiting the Program‬‭:‬
‭●‬ ‭Choose this option to exit the program gracefully.‬

‭Note:‬
‭‬ E
● ‭ nsure to input valid resistance values (greater than zero) when adding resistors.‬
‭●‬ ‭When calculating the total resistance, choose either "series" or "parallel" connection type.‬
‭●‬ ‭Any invalid input or choices will prompt you to try again.‬

‭ lease provide a screenshot of your program. Remember, it's essential to maintain originality when completing this‬
P
‭task.‬

‭Leading Innovations, Transforming Lives, Building the Nation‬


‭Republic of the Philippines‬
‭BATANGAS STATE UNIVERSITY‬
‭The National Engineering University‬
‭Alangilan Campus‬
‭Golden Country Homes, Alangilan Batangas City, Batangas, Philippines 4200‬
‭Tel Nos.: (+63 43) 425-0139 local 2121 / 2221‬
‭ -mail Address:‬‭ceafa@g.batstate-u.edu.ph | Website‬‭Address:‬‭http://www.batstate-u.edu.ph‬
E

‭CODE‬

‭ essage = "Resistance Manager and Total Resistance‬


m ‭total_resistance = 1 / sum(1 / r for r in‬
‭Calculator \n"‬ ‭self.resistors)‬
‭centered_message = message.center(140)‬ ‭print(f"Total resistance in parallel‬
‭print(centered_message)‬ ‭connection: {total_resistance:.2f} ohms")‬
‭break‬
‭class ResistorManager:‬ ‭else:‬
‭def __init__(self):‬ ‭print("Invalid connection type. Please enter‬
‭self.resistors = []‬ ‭'series' or 'parallel'.")‬

‭def add_resistor(self):‬ ‭def display_menu(self):‬


‭while True:‬ ‭print("\nOptions:")‬
‭try:‬ ‭print("1. Add a resistor")‬
‭resistance = float(input("Enter the‬ ‭print("2. Calculate total resistance")‬
‭resistance value of the resistor: "))‬ ‭print("3. Exit \n")‬
‭if resistance <= 0:‬
‭raise ValueError("Resistance value must‬ ‭def run(self):‬
‭be greater than zero.")‬ ‭while True:‬
‭self.resistors.append(resistance)‬ ‭self.display_menu()‬
‭print("Resistor added successfully.")‬ ‭choice = input("Enter your choice (1-3): ")‬
‭break‬ ‭if choice == "1":‬
‭except ValueError as ve:‬ ‭self.add_resistor()‬
‭print(f"Error: {ve}")‬ ‭elif choice == "2":‬
‭self.calculate_total_resistance()‬
‭def calculate_total_resistance(self):‬ ‭elif choice == "3":‬
‭if not self.resistors:‬ ‭print("Exiting the program. Goodbye!")‬
‭print("No resistors have been added yet.")‬ ‭break‬
‭return‬ ‭else:‬
‭print("Invalid choice. Please enter a‬
‭while True:‬ ‭number between 1 and 3.")‬
‭connection_type = input("Enter the‬
‭connection type (series or parallel): ").lower()‬
‭if connection_type == "series":‬ #‭ Create an instance of ResistorManager and run the‬
‭total_resistance = sum(self.resistors)‬ ‭program‬
‭print(f"Total resistance in series‬ ‭resistor_manager = ResistorManager()‬
‭connection: {total_resistance:.2f} ohms")‬ ‭resistor_manager.run()‬
‭break‬
‭elif connection_type == "parallel":‬

‭Leading Innovations, Transforming Lives, Building the Nation‬


‭Republic of the Philippines‬
‭BATANGAS STATE UNIVERSITY‬
‭The National Engineering University‬
‭Alangilan Campus‬
‭Golden Country Homes, Alangilan Batangas City, Batangas, Philippines 4200‬
‭Tel Nos.: (+63 43) 425-0139 local 2121 / 2221‬
‭ -mail Address:‬‭coe.alangilan@g.batstate-u.edu.ph‬‭| Website Address:‬‭http://www.batstate-u.edu.ph‬
E

‭College of Engineering – Department of Civil Engineering‬

‭Leading Innovations, Transforming Lives, Building the Nation‬

You might also like