-
Notifications
You must be signed in to change notification settings - Fork 420
/
Copy pathelectric_values_converter.py
executable file
·95 lines (79 loc) · 2.84 KB
/
electric_values_converter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
def convert_electric_values():
"""
This program compute electric values. User enter 2 values and
the program compute the unknown values.
"""
print("Enter 2 of this 4 following values:")
print("1. Current (I -> A)")
print("2. Voltage (V -> V)")
print("3. Resitance (R -> Ω)")
print("4. Power (P -> W)")
I = None
V = None
R = None
P = None
while True:
input_number = 0
try:
I_input = input("Enter current value (I -> A) or hit 'enter' if unknown : ")
if I_input.strip() != "":
I = float(I_input)
input_number += 1
V_input = input("Enter voltage value (V -> V) or hit 'enter' if unknown : ")
if V_input.strip() != "":
V = float(V_input)
input_number += 1
R_input = input("Enter resitance value (R -> Ω) or hit 'enter' if unknown : ")
if R_input.strip() != "":
R = float(R_input)
input_number += 1
P_input = input("Enter power value (P -> W) or hit 'enter' if unknown : ")
if P_input.strip() != "":
P = float(P_input)
input_number += 1
if(input_number > 2):
print("You have entered more than 2 values! Please enter exactly 2 values")
continue
if(input_number < 2):
print("You have entered less than 2 values! Please enter exactly 2 values")
continue
break
except ValueError:
print("Please enter a valid value!")
# Calcul des valeurs manquantes
if I is not None and V is not None:
R = V / I
P = I * V
elif I is not None and R is not None:
V = I * R
P = I * V
elif V is not None and R is not None:
I = V / R
P = I * V
elif I is not None and V is not None:
P = I * V
R = V / I
elif I is not None and P is not None:
V = P / I
R = V / I if I != 0 else float('inf')
elif V is not None and P is not None:
I = P / V
R = V / I if I != 0 else float('inf')
elif R is not None and P is not None:
I = P / (V if V is not None else (R * I if I is not None else float('inf')))
V = P / I if I != 0 else float('inf')
print("\nResults :")
if I is not None:
print(f"Current (I) : {I:.2f} A")
if V is not None:
print(f"Voltage (V) : {V:.2f} V")
if R is not None:
print(f"Resistance (R) : {R:.2f} Ω")
if P is not None:
print(f"Power (P) : {P:.2f} W")
if __name__ == "__main__":
while True:
convert_electric_values()
convert_again = input("\nConvert again? yes / no: ").lower()
if convert_again != "yes":
break