Python(파이썬) - 옴의 법칙 이용해서 전압, 전류, 저항 구하기
2023. 3. 29. 00:04ㆍ파이썬(Python)
옴의 법칙 이용해서 전압, 전류, 저항 구하기
대학 다니면서 옴의 법칙을 자주 쓸거 같아서 파이썬으로 계산하는 프로그램을 만들어 보았다.
프로그램 실행 순서
1. 프로그램을 실행한 후 전압, 전류, 저항 중에 구하고 싶은 값을 숫자로 입력한다.
2. 각각 전압 or 전류 or 저항값을 단위에 맞게 입력한다.
3. 구하고 싶은 값이 출력된다.
한국어 버전
# 옴의 법칙 이용해서 전압, 전류, 저항 구하기
try:
choice = int(input("어떤 값을 구하려고 하나요? (1: 전압(V), 2: 전류(I), 3: 저항(R): "))
if choice == 1: # 전압 구하기
current = float(input("전류(I)를 [mA]단위로 입력하세요: "))
resistance = float(input("저항(R)을 [Ω]단위로 입력하세요: "))
voltage = current * resistance
print(f"전압(V)은 {round(voltage/1000000000, 2)}[MV], {round(voltage/1000000, 2)}[kV], {round(voltage/1000, 2)}[V], {round(voltage, 2)}[mV], {round(voltage*1000, 2)}[µV] 입니다.")
elif choice == 2: # 전류 구하기
voltage = float(input("전압(V)을 [V]단위로 입력하세요: "))
resistance = float(input("저항(R)을 [Ω]단위로 입력하세요: "))
current = voltage / resistance
print(f"전류(I)는 {round(current/1000000, 2)}[MA], {round(current/1000, 2)}[kA], {round(current, 2)}[A], {round(current*1000, 2)}[mA], {round(current*1000000, 2)}[µA] 입니다.")
elif choice == 3: # 저항 구하기
voltage = float(input("전압(V)을 [V]단위로 입력하세요: "))
current = float(input("전류(I)를 [mA]단위로 입력하세요: "))
resistance = voltage / current
print(f"저항(R)은 {round(resistance/1000, 2)}[MΩ], {round(resistance, 2)}[kΩ], {round(resistance*1000, 2)}[Ω], {round(resistance*1000000, 2)}[mΩ], {round(resistance*1000000000, 2)}[µΩ] 입니다.")
except ValueError: # 숫자가 아닌 다른 값이 입력되었을 때
print("ValueError: 올바른 숫자를 입력해주세요.")
영어 버전
# Using Ohm's law to calculate voltage, current, and resistance
try:
choice = int(input("What value would you like to calculate? (1: Voltage(V), 2: Current(I), 3: Resistance(R): "))
if choice == 1: # Calculate voltage
current = float(input("Enter the current (I) in [mA]: "))
resistance = float(input("Enter the resistance (R) in [Ω]: "))
voltage = current * resistance
print(f"The voltage (V) is {round(voltage/1000000000, 2)}[MV], {round(voltage/1000000, 2)}[kV], {round(voltage/1000, 2)}[V], {round(voltage, 2)}[mV], {round(voltage*1000, 2)}[µV].")
elif choice == 2: # Calculate current
voltage = float(input("Enter the voltage (V) in [V]: "))
resistance = float(input("Enter the resistance (R) in [Ω]: "))
current = voltage / resistance
print(f"The current (I) is {round(current/1000000, 2)}[MA], {round(current/1000, 2)}[kA], {round(current, 2)}[A], {round(current*1000, 2)}[mA], {round(current*1000000, 2)}[µA].")
elif choice == 3: # Calculate resistance
voltage = float(input("Enter the voltage (V) in [V]: "))
current = float(input("Enter the current (I) in [mA]: "))
resistance = voltage / current
print(f"The resistance (R) is {round(resistance/1000, 2)}[MΩ], {round(resistance, 2)}[kΩ], {round(resistance*1000, 2)}[Ω], {round(resistance*1000000, 2)}[mΩ], {round(resistance*1000000000, 2)}[µΩ].")
except ValueError: # Handle non-numeric inputs
print("ValueError: Please enter a valid number.")