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
Tags:
将长if语句转换为字典值。然后检查字典中的键
代码中的一些错误:
您有代码
use (1.0005)
。这是什么?你打算用它做什么?是否要将1.0005
的值分配给k1
?如果是这样,您需要执行k1=(1.0005)
您还将
k1
与E0
相乘。不能将字符串与浮点值相乘。只能将字符串与整数相乘我建议对代码进行如下更改
使用以下代码:
代码的示例输出:
快速且“肮脏”的修复: 您可以将代码中的“use”替换为“k1=”。。。 这将把k1值设置为您期望的值,代码将正常工作
正如其他评论所建议的那样,您仍然可以改进编码风格
它起作用了,结果如下:
对于另一个错误,我可以在这里向您展示缩进的正确方式, 但是我没有您的代码的剩余部分,并且在注释中很难阅读:因此,这里是一个尝试:
缩进应为4个空格,如下所示:
相关问题 更多 >
编程相关推荐