A1= float(input("What is the value of the area of the plates used in the first capacitor ? n"))
if A1 == 0:
print("The area cannot be zero.")
raise SystemExit
d1= float(input("What is the separation between the plates used in the first capacitor ? n"))
if d1 == 0:
print("The distance cannot be zero.")
raise SystemExit
k1= input ('What is the medium used between the plates of first capacitor (vacuum, Air, polystyrene, paper, silicon, pyrex glass, porcelain, nerve membrane, ethanol, water n)')
if k1== 'vacuum':
print (1)
elif k1 == 'Air':
use (1.0005)
elif k1 == 'polystyrene':
use (2.6)
elif k1 == 'paper':
use (3.5)
elif k1 == 'silicon':
use (12.0)
elif k1 == 'pyrex glass':
use (4.7)
elif k1 == 'porcelain':
use (6.5)
elif k1 == ' nerve membrane':
use (7.0)
elif k1 == 'ethanol':
use (25.0)
elif k1 == 'water':
use (78.5)
E0= 8.85*10**-12
C1= k1* E0*A1/d1
快速且"脏的";修复:您可以将";使用";通过";k1=";在您的代码中。。。这将把k1值设置为您所期望的值,代码就可以工作了。
正如其他评论所建议的那样,您仍然可以改进编码样式。
A1= float(input("What is the value of the area of the plates used in the first capacitor ? n"))
if A1 == 0:
print("The area cannot be zero.")
raise SystemExit
d1= float(input("What is the separation between the plates used in the first capacitor ? n"))
if d1 == 0:
print("The distance cannot be zero.")
raise SystemExit
k1= input ('What is the medium used between the plates of first capacitor (vacuum, Air, polystyrene, paper, silicon, pyrex glass, porcelain, nerve membrane, ethanol, water n)')
if k1== 'vacuum':
print (1)
elif k1 == 'Air':
k1=(1.0005)
elif k1 == 'polystyrene':
k1=(2.6)
elif k1 == 'paper':
k1=(3.5)
elif k1 == 'silicon':
k1=(12.0)
elif k1 == 'pyrex glass':
k1=(4.7)
elif k1 == 'porcelain':
k1=(6.5)
elif k1 == ' nerve membrane':
k1=(7.0)
elif k1 == 'ethanol':
k1=(25.0)
elif k1 == 'water':
k1=(78.5)
E0= 8.85*10**-12
C1= k1* E0*A1/d1
print ('E0=', E0, 'C1=', C1)
它有效,结果如下:
What is the value of the area of the plates used in the first capacitor ?
5
What is the separation between the plates used in the first capacitor ?
2
What is the medium used between the plates of first capacitor (vacuum, Air, polystyrene, paper, silicon, pyrex glass, porcelain, nerve membrane, ethanol, water
)water
E0= 8.849999999999999e-12 C1= 1.7368124999999997e-09
对于另一个错误,我可以在这里向您展示正确的缩进方式,但我没有你的代码的reste,很难在评论中阅读:所以这里有一个尝试:
缩进应为4个空格,如下所示:
if Ceq == 'series':
Ceq = (C1**-1 + C2**-1)**-1
elif Ceq == 'parallel':
Ceq= C1+ C2
将长if语句转换为字典值。然后查字典里的钥匙。
你的代码中有几个错误:
-
您有代码
use (1.0005)
。这是什么?你打算用它做什么?是否要将1.0005
的值分配给k1
?如果是,则需要执行k1=(1.0005)
-
您还将
k1
与E0
相乘。不能将字符串与浮点值相乘。只能将字符串与整数相乘。
我建议对您的代码进行如下所示的更改。
使用以下代码:
k= {'vacuum':1,
'Air':1.0005,
'polystyrene':2.6,
'paper':3.5,
'silicon':12.0,
'pyrex glass':4.7,
'porcelain':6.5,
'nerve membrane':7.0,
'ethanol':25.0,
'water':78.5}
A1= float(input("What is the value of the area of the plates used in the first capacitor ? n"))
if A1 == 0:
print("The area cannot be zero.")
raise SystemExit
d1= float(input("What is the separation between the plates used in the first capacitor ? n"))
if d1 == 0:
print("The distance cannot be zero.")
raise SystemExit
k1= input ('What is the medium used between the plates of first capacitor (vacuum, Air, polystyrene, paper, silicon, pyrex glass, porcelain, nerve membrane, ethanol, water n)')
if k1 in k:
E0= 8.85 * 10 ** -12
C1= k[k1] * E0 * A1/d1
print ('E0 : ', E0, 'C1 : ', C1)
else:
print ('Invalid medium entered')
代码输出示例:
What is the value of the area of the plates used in the first capacitor ?
2
What is the separation between the plates used in the first capacitor ?
3
What is the medium used between the plates of first capacitor (vacuum, Air, polystyrene, paper, silicon, pyrex glass, porcelain, nerve membrane, ethanol, water
)water
E0 : 8.849999999999999e-12 C1 : 4.6314999999999995e-10