我得到类型错误:不能将序列乘以“float”类型的非整数,有什么建议吗?

2024-04-26 12:08:08 发布

您现在位置:Python中文网/ 问答频道 /正文

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: oftheinputifisusea1k1
2条回答

将长if语句转换为字典值。然后检查字典中的键

代码中的一些错误:

  1. 您有代码use (1.0005)。这是什么?你打算用它做什么?是否要将1.0005的值分配给k1?如果是这样,您需要执行k1=(1.0005)

  2. 您还将k1E0相乘。不能将字符串与浮点值相乘。只能将字符串与整数相乘

我建议对代码进行如下更改

使用以下代码:

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

快速且“肮脏”的修复: 您可以将代码中的“use”替换为“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

对于另一个错误,我可以在这里向您展示缩进的正确方式, 但是我没有您的代码的剩余部分,并且在注释中很难阅读:因此,这里是一个尝试:

缩进应为4个空格,如下所示:

if Ceq == 'series':
    Ceq = (C1**-1 + C2**-1)**-1
elif Ceq == 'parallel':
    Ceq= C1+ C2

相关问题 更多 >