Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

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
E-mail Address: coe.alangilan@g.batstate-u.edu.ph | Website Address: http://www.batstate-u.edu.ph

College of Engineering - Department of Electrical Engineering

Bachelor of Science in Computer Engineering

CpE 401: COMPUTER PROGRAMMING 1


2nd Semester A.Y. 2023 – 2024

Activity 3
LOOPS

SUBMITTED BY:
Abrigo, Renz Christian, B.

COE - 1203

SUBMITTED TO:
Sir Mark Rondol P. Abdon

Leading Innovations, Transforming Lives, Building the Nation


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:
● Ensure 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.


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
E-mail Address: coe.alangilan@g.batstate-u.edu.ph | Website Address: http://www.batstate-u.edu.ph

College of Engineering - Department of Electrical Engineering

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

Leading Innovations, Transforming Lives, Building the Nation


Paste your code here use comments to properly document your program.
resistor_list = []

def add_resistor(): #Add a resistor


resistance_value = float(input("Enter resistance value in ohms: ")) #Enter the resistance value of the resistor when
prompted.
#If the resistance value is greather than zero, it will be added to the list of values of resistors and a confirmation
message will be displayed.
if resistance_value <= 0:
print("Error. Please input a value greater than zero.")
else:
resistor_list.append(resistance_value)
print("Added to list of Resistors.")

def calc_total(): #Calculate total resistance


if resistor_list == []: #If no resistors have been added yet, the program will display a message indicating that no
resistors have been added.
print("There are no resistors added.")
else: #If a resistor is present, the user will choose between series or parallel.
print("1. Series")
print("2. Parallel")
con_type = int(input("Choose a connection type (1 or 2): "))
if con_type == 1: #Series: 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.
total_value = round((sum(resistor_list)), 2)
print("Total Resistance Value:", total_value)
elif con_type == 2: #Paralle: The program will calculate the total resistance using the reciprocal of the sum of
reciprocals of all resistances.The total resistance value will be displayed with two decimal places.
total_reciprocal = 0
for value in resistor_list:
reciprocal = 1/value
total_reciprocal += reciprocal
total_resistance = round((1/total_reciprocal), 2)
print("Total Resistance Value:", total_resistance)

def main():
while True: #Your program must present the options repeatedly
print("Resistor Manager and Total Resistance Calculator")
print("1. Add a Resistor")
print("2. Calculate Total Resistance")
print("3. Exit")
choice = int(input("Enter option: "))
if choice == 1: #Choose this option to calculate the total resistance of the circuit.
print("\n")
add_resistor()
elif choice == 2: #Choose this option to add a resistor to the circuit.
print("\n")
calc_total()
elif choice == 3: #Choose this option to exit the program.
print("\n")
print("Exiting program.") #Exit the program
break
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
E-mail Address: coe.alangilan@g.batstate-u.edu.ph | Website Address: http://www.batstate-u.edu.ph

College of Engineering - Department of Electrical Engineering

else: #Any invalid input or choices will prompt you to try again.
print("Please pick from options 1-3 only.")

main()

Snapshot of the python code

Leading Innovations, Transforming Lives, Building the Nation


Snapshots of the output
Adding resistors and choosing Series: Adding resistors and choosing Parallel:

If resistance value is less than zero: If the user did not pick from options 1-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
E-mail Address: coe.alangilan@g.batstate-u.edu.ph | Website Address: http://www.batstate-u.edu.ph

College of Engineering - Department of Electrical Engineering

If no resistors are added: If you just want to exit the program:

Leading Innovations, Transforming Lives, Building the Nation

You might also like