“TypeError:不能将序列与'float'类型的nonint相乘”,即使输入到float的转换是don

2024-04-26 15:02:13 发布

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

这是我的全部密码-

def Inputs():

    global l,n,A,k,TA,TB,q,A,C,T,x

    l = float(input("Enter the Total Length of the Element AB : "))
    n = 1
    while (n <= 1):
        n = int(input("Enter the No. of Nodes (> 1) : "))
    A = float(input("Enter the Area of Cross-Section : "))
    k = float(input("Enter the value of Thermal Conductivity : "))
    TA = float(input("Enter the Temperature at the left end : "))
    TB = float(input("Enter the Temperature at the right end : "))
    q = float(input("Enter the value of Heat Supplied (Source Term) : "))

def Initialization():

    A = n*[n*[0]]
    C = n*[0]
    T = n*[0]
    x = l/n

    # For Node 1
    A[0][0] = (3.0*k*A)/x
    A[0][1] = (-1.0*k*A)/x
    C[0] = ((2.0*k*A*TA)/x) + (q*A*x)

    # For Node n
    A[n-1][n-2] = (3.0*k*A)/x
    A[n-1][n-1] = (-1.0*k*A)/x
    C[n-1] = ((2.0*k*A*TB)/x) + (q*A*x)

    # For Nodes 2 to (n-1)
    for i in range(1,n-1):
        A[i][i-1] = (-1.0*k*A)/x
        A[i][i] = -2.0*A[i][i-1]
        A[i][i+1] = A[i][i-1]
        C[i] = q*A*x

def ThomasAlgorithm():

    A[0][1] /= A[0][0]
    C[0] /= A[0][0]
    A[0][0] = 1
    for i in range(1,n-1):
        k1 = A[i][i-1]
        A[i][i-1] -= k1*A[i-1][i-1]
        A[i][i] -= k1*A[i-1][i]
        A[i][i+1] -= k1*A[i-1][i+1]
        k2 = A[i][i]
        C[i] -= k1*C[i-1]
        A[i][i] /= k2
        A[i][i+1] /= k2
        C[i] /= k2

def Calculations():

    T[n-1] = C[n-1]
    for i in range(n-2,-1):
        T[i] = C[i] - (A[i][i+1]*T[i+1])

def main():

    Inputs()
    Initialization()
    ThomasAlgorithm()
    Calculations()
    for i in range(n):
        print('T{0} = {1:.3f}\n'.format(i+1,T[i]))

运行代码后,我得到了这个错误

>>> main()

Enter the Total Length of the Element AB : 0.02

Enter the No. of Nodes (> 1) : 4

Enter the Area of Cross-Section : 1

Enter the value of Thermal Conductivity : 5

Enter the Temperature at the left end : 100

Enter the Temperature at the right end : 400

Enter the value of Heat Supplied (Source Term) : 500

Traceback (most recent call last):
    File "<pyshell#0>", line 1, in <module>
        main()
    File "C:/Python33/CFD_1.py", line 58, in main
        Initialization()
    File "C:/Python33/CFD_1.py", line 20, in Initialization
        A[0][0] = (3.0*k*A)/x
        TypeError: can't multiply sequence by non-int of type 'float'

有人能帮我找错地方吗?我知道输入必须转换成float。 我也犯了同样的错误。非常感谢。你知道吗


Tags: oftheinforinputvaluedefrange
1条回答
网友
1楼 · 发布于 2024-04-26 15:02:13

A最初是一个浮点:

A = float(input("Enter the Area of Cross-Section : "))

但是你把它重新声明为一个列表列表:

A = n*[n*[0]]

您应该为这两个实体使用不冲突的名称。你知道吗

相关问题 更多 >