0

This is my entire code -

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]))

After running the code I got this error

>>> 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'

Can somebody help me find my mistake? I am aware that input must be converted to float. I have done the same but still this error. Thank you.

DOOM
  • 1,170
  • 6
  • 20
Suhas V
  • 11
  • 1
  • 3
    The thing is, you don't have to provide **the entire code**. [SSCCE](http://sscce.org/). – vaultah Sep 14 '14 at 10:16
  • Also, you probably want to read [How do I format my posts using Markdown or HTML?](http://stackoverflow.com/help/formatting) – Tim Pietzcker Sep 14 '14 at 10:18
  • You are literally trying to access two different objects of different types via the same name within the failing line - what on earth did you expect would happen?! – jonrsharpe Sep 14 '14 at 10:28
  • Is my use of global variables correct? – Suhas V Sep 14 '14 at 10:34
  • 1
    Use of `global` is rarely correct. Used explicit arguments, either separately or in a container (e.g. a dictionary). – jonrsharpe Sep 14 '14 at 10:58

1 Answers1

2

A is initially a float:

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

But then you redeclare it as a list of lists:

A = n*[n*[0]]

You should use non-conflicting names for the two entities.

amgaera
  • 1,838
  • 1
  • 15
  • 15
  • 1
    Also, `A = n*[n*[0]]` almost certainly is not what @SuhasV actually wants to do because it results in `n` references to a *single* list consisting of `n` zeroes... – Tim Pietzcker Sep 14 '14 at 10:24
  • Thanks a lot :) I need a matrix of order nxn filled with zeroes – Suhas V Sep 14 '14 at 10:27
  • @SuhasV - it's unidiomatic to say the least. I'd probably pass around a named tuple with all the arguments you need instead. – amgaera Sep 14 '14 at 10:57
  • @SuhasV: For a true `n` x `n` matrix, you need to do `A = [n*[0] for _ in range(n)]`. – Tim Pietzcker Sep 14 '14 at 11:23
  • I would like to convert the same into a portable .exe file. How do i do it? Please help me. – Suhas V Sep 14 '14 at 12:47
  • @SuhasV: If you search StackOverflow for "python exe", you'll find many answers such as http://stackoverflow.com/questions/16770267/how-can-i-turn-a-python-3-3-script-into-executable-file-i-found-pyinstaller-and – Tim Pietzcker Sep 14 '14 at 16:31