这是我的整个代码 -
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。 我做了同样但仍然是这个错误。谢谢。
答案 0 :(得分:2)
A
最初是一个浮点数:
A = float(input("Enter the Area of Cross-Section : "))
但是你将它重新声明为列表清单:
A = n*[n*[0]]
您应该为这两个实体使用非冲突名称。