理想气体类型错误:不能将序列与“float”3.4.4类型的非整数相乘?

2024-09-25 00:34:44 发布

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

我想制作一个程序来绘制两种理想气体的图形,但炮弹发射时出现了以下错误:

line 10, in P1
    return (P*(Vn[c]))/(T[c2])
TypeError: can't multiply sequence by non-int of type 'float'

这是我的节目:

  #Prueba de gráfica de gas ideal con volumen molar
import numpy as np
from matplotlib import pyplot as plt    
#Sea Vn=miu/densidad... VnNeón=16.82 ml/mol, VnCriptón=32.23 ml/mol
Vn=[16.82,32.23]
T=[0.01,60,137,258]
c=0 #contador del material
c2=0 #contador temperatura
def P1(P): #Función de P:
    return (P*(Vn[c]))/(T[c2])
P= list(range(0,800))
while c<=1:
    while c2<=3:
        print(P1(P),Vn[c],T[c2])
        c2=c2+1    
    c=c+1

我能做什么? 我在windows 10中使用Python3.4.4。我想得到一个P1的图表,它依赖于P(P从0到800),对于列表T中的每个温度,对于列表Vn中的每个氖和克里普顿摩尔体积。 为什么我不能用P来乘和除列表中的元素? 非常感谢你


Tags: import程序列表returnas绘制deml
1条回答
网友
1楼 · 发布于 2024-09-25 00:34:44

稍微调试一下会有很大帮助。将函数更改为

def P1(P): #Función de P:
    print(type(P), type(Vn[c]), type(T[c2]))
    return (P*(Vn[c]))/(T[c2])

运行它会打印

<class> 'list' <class 'float'> <class 'float'>

您试图将一个list与两个floats相乘,这显然不起作用P = list(range(0, 800)),因此您需要使用一些索引。我不确定您想做什么,但作为一个例子,以下函数对我来说运行良好:

def P1(P): #Función de P:
    #         | just added an index here
    return (P[0]*(Vn[c]))/(T[c2])

相关问题 更多 >