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

# Richmond Alexander M.

Resuello, AeE - 1202


# Resistor Manager and Total Resistance Calculator
print("Welcome to Resistor Manager and Total Resistance Calculator Program")

resistors = []

while True:
print("\nChoose a number in the following options.")
print("Options:")
print(" 1. Add a resistor")
print(" 2. Calculate total resistance")
print(" 3. Exit")

choice = input("\nEnter the number of your choice: ")

if choice == "1":
# Add a resistor
resistance = input("Enter the resistance value: ")

#Identify if Input is a valid characters ('0' to '9' and '.') and is


greater than 0.
if resistance.replace('.', '', 1).isdigit() and float(resistance) > 0:
# Add to the list of resistors, and a confirmation message will be
displayed.
resistors.append(float(resistance))
print("Resistor added successfully!")
else:
# If the resistance value is not valid or less than or equal to
zero, an appropriate error message will be displayed.
print("Invalid resistance value. Please try again.")

elif choice == "2":


# Calculate total resistance
# If no resistors have been added yet, the program will display a message
indicating that no resistors have been added.
if not resistors:
print("No resistors have been added yet.")

else:
# The user must be prompted to enter the connection type (series or
parallel).
connection_type = input("Enter the connection type (series or
parallel): ")
# 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.
if connection_type.lower() == "series":
total_resistance = sum(resistors)
print(f"Total resistance: {total_resistance:.2f} ohms")

# 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.
elif connection_type.lower() == "parallel":
reciprocal_sum = 0
for r in resistors:
reciprocal_sum += 1/r
total_resistance = 1 / reciprocal_sum
print(f"Total resistance: {total_resistance:.2f} ohms")
break

elif choice == "3":


# Exit the program
print("Exiting the program. Goodbye!")
break

else:
print("Invalid choice. Please try again.")

You might also like