Python“AttributeError:”str'对象没有属性“Tc”(Tc是参数之一)

2024-06-17 11:10:43 发布

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

我有这个代码:

import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import newton

R = 8.314e-5  # universal gas constant, m3-bar/K-mol
class Molecule:
"""
Store molecule info here
"""
def __init__(self, name, Tc, Pc, omega):
    """
    Pass parameters desribing molecules
    """
    #! name
    self.name = methane
    #! Critical temperature (K)
    self.Tc = -83+273
    #! Critical pressure (bar)
    self.Pc = 45.99
    #! Accentric factor
    self.omega = 0.011

def preos(molecule, T, P, plotcubic=True, printresults=True):
    Tr = T / molecule.Tc  # reduced temperature
    a = 0.457235 * R**2 * molecule.Tc**2 / molecule.Pc
    b = 0.0777961 * R * molecule.Tc / molecule.Pc
    kappa = 0.37464 + 1.54226 * molecule.omega - 0.26992 * molecule.omega**2
    alpha = (1 + kappa * (1 - np.sqrt(Tr)))**2

    A = a * alpha * P / R**2 / T**2
    B = b * P / R / T

当我调用函数时,我需要的参数是:

^{pr2}$

出现错误消息:“AttributeError:'str'object has no attribute'Tc'”(此部件上有一条错误消息:“AttributeError:'str'object has no attribute'Tc'”:

def preos(molecule, T, P, plotcubic=True, printresults=True):
    Tr = T / molecule.Tc  # reduced temperature

我想其他参数(Pc和omega)也会有同样的错误。这个错误是什么意思?在


Tags: nameimportselftruedefas错误np
1条回答
网友
1楼 · 发布于 2024-06-17 11:10:43

在这里:

def preos(molecule, T, P, plotcubic=True, printresults=True):
    Tr = T / molecule.Tc  # reduced temperature
...
preos("methane", 160, 10, "true", "true")

很明显,您将“甲烷”作为字符串传递到preos函数中,然后尝试在该字符串上调用.Tc。错误就是这么说的。这和IPython没有任何关系。换句话说,您正在尝试运行"methane".Tc。在

编辑:很难说你到底想发生什么,但我认为你没有得到足够的类和方法。在

相关问题 更多 >