03. Circuit Analysis(Sum of the series and parallel resistors)
03. Circuit Analysis(Sum of the series and parallel resistors)
[toc]
2022-10-10 회로이론 과제
파이썬으로 직렬-병렬 저항의 합 구하는 코드 구현
코드
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
ID = input("What is your Student ID? ")
#A and B
AtoB = input(
"Are Registers between Node A and B connected Series or Parallel (S/P)? ")
if AtoB == 'S':
Ssum1 = int(input("How many registers are connected in Series? "))
if Ssum1 == 0:
ValofS1 = []
ValofS1.append(0)
AtoB_Sum = sum(ValofS1)
else:
ValofS1 = list(
map(int, input("Please, type the values of registers: ").split(", ")))
# print(ValofS1)
AtoB_Sum = 0
if Ssum1 == len(ValofS1):
AtoB_Sum = sum(ValofS1)
else:
raise
# print(AtoB_Sum)
elif AtoB == 'P':
Psum1 = int(input("How many registers are connected in Parallel? "))
if Psum1 == 0:
ValofP1 = []
ValofP1.append(0)
AtoB_Sum = sum(ValofP1)
else:
ValofP1 = list(
map(int, input("Please, type the val8ues of registers: ").split(", ")))
# print(ValofP1)
i = 0
AtoB_Sum = 0
while i < Psum1:
if Psum1 == len(ValofP1):
if ValofP1[i] == 0:
AtoB_Sum += 0
i += 1
else:
AtoB_Sum += 1/ValofP1[i]
i += 1
else:
raise
if AtoB_Sum == 0:
AtoB_Sum = 0
else:
AtoB_Sum = 1/AtoB_Sum
# print(AtoB_Sum)
else:
raise
#B and C
BtoC = input(
"Are Registers between Node B and C connected Series or Parallel (S/P)? ")
if BtoC == 'S':
Ssum2 = int(input("How many registers are connected in Series? "))
if Ssum2 == 0:
ValofS2 = []
ValofS2.append(0)
BtoC_Sum = sum(ValofS2)
else:
ValofS2 = list(
map(int, input("Please, type the values of registers: ").split(", ")))
# print(ValofS2)
BtoC_Sum = 0
if Ssum2 == len(ValofS2):
BtoC_Sum = sum(ValofS2)
else:
raise
# print(BtoC_Sum)
elif BtoC == 'P':
Psum2 = int(input("How many registers are connected in Parallel? "))
if Psum2 == 0:
ValofP2 = []
ValofP2.append(0)
BtoC_Sum = sum(ValofP2)
else:
ValofP2 = list(
map(int, input("Please, type the values of registers: ").split(", ")))
# print(ValofP2)
i = 0
BtoC_Sum = 0
while i < Psum2:
if Psum2 == len(ValofP2):
if ValofP2[i] == 0:
BtoC_Sum += 0
i += 1
else:
BtoC_Sum += 1/ValofP2[i]
i += 1
else:
raise
if BtoC_Sum == 0:
BtoC_Sum = 0
else:
BtoC_Sum = 1/BtoC_Sum
# print(BtoC_Sum)
else:
raise
#C and D
CtoD = input(
"Are Registers between Node C and D connected Series or Parallel (S/P)? ")
if CtoD == 'S':
Ssum3 = int(input("How many registers are connected in Series? "))
if Ssum3 == 0:
ValofS3 = []
ValofS3.append(0)
CtoD_Sum = sum(ValofS3)
else:
ValofS3 = list(
map(int, input("Please, type the values of registers: ").split(", ")))
# print(ValofS3)
CtoD_Sum = 0
if Ssum3 == len(ValofS3):
CtoD_Sum = sum(ValofS3)
else:
raise
# print(CtoD_Sum)
elif CtoD == 'P':
Psum3 = int(input("How many registers are connected in Parallel? "))
if Psum3 == 0:
ValofP3 = []
ValofP3.append(0)
CtoD_Sum = sum(ValofP3)
else:
ValofP3 = list(
map(int, input("Please, type the values of registers: ").split(", ")))
# print(ValofP3)
i = 0
CtoD_Sum = 0
while i < Psum3:
if Psum3 == len(ValofP3):
if ValofP3[i] == 0:
CtoD_Sum += 0
i += 1
else:
CtoD_Sum += 1/ValofP3[i]
i += 1
else:
raise
if CtoD_Sum == 0:
CtoD_Sum = 0
else:
CtoD_Sum = 1/CtoD_Sum
# print(CtoD_Sum)
else:
raise
Rsum = AtoB_Sum + BtoC_Sum + CtoD_Sum
print("\n")
print("%s, the equivalent register value of the circuit is %0.1f Ω" % (ID, Rsum))
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
결과
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
What is your Student ID? abcdef
Are Registers between Node A and B connected Series or Parallel (S/P)? S
How many registers are connected in Series? 2
Please, type the values of registers: 10, 20
Are Registers between Node B and C connected Series or Parallel (S/P)? P
How many registers are connected in Parallel? 3
Please, type the values of registers: 10, 20, 30
Are Registers between Node C and D connected Series or Parallel (S/P)? P
How many registers are connected in Parallel? 3
Please, type the values of registers: 10, 20, 30
abcdef, the equivalent register value of the circuit is 40.9 Ω
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- 함수를 썼으면 더 간단하게 할 수 있는 것을 구현하지 못함