J.Gladney - 1 month ago 10

Python Question

So I'm building a matrix calculator without NumPy and I have a limited but working model. However that model is very rough and I would like to improve upon it as well as expand its functionality to be able to compute multiple matrices and interact with each other (multiplication for example).

A problem I have quickly run into with my very limited experience with Python is the passing of variables between multiple functions (parameters I believe they're called). Here is what I have:

`def matrixGenerator1():`

rows = int(input("\nHow many rows are in the matrix?\n\n:"))

columns = int(input("\nHow many columns are in the matrix?\n\n:"))

matrix1 = [[0 for x in range(columns)] for y in range(rows)]

for y in range(rows):

for x in range(columns):

matrix1[y][x] = int(input("\nWhat is the entry?\n\n:"))

print("\nThe original matrix is...")

for y in range(rows):

print("\n")

for x in range(columns):

print(format(matrix1[y][x], "5d"), end="")

return rows, columns, matrix1

def matrixTranspose(rows, columns, matrix1):

print("\nThe transpose of this matrix is...")

for y in range(rows):

print("\n")

for x in range(columns):

print(format(matrix1[y][x], "5d"), end="")

matrixGenerator1()

matrixTranspose(rows, columns, matrix1)

Running this results in an error message saying that

`rows`

`columns`

`matrix1`

`matrixGenerator`

Is there anyway that someone could provide me a general explanation as to what I'm doing wrong so I can prevent this error in the future?

Answer

You need to save the output of your first function in your main routine. (Notice you are already returning it so you are almost done):

```
rows, columns, matrix1 = matrixGenerator1()
matrixTranspose(rows, columns, matrix1)
```

Source (Stackoverflow)

Comments