简体   繁体   中英

I'm trying to condense my code with while loops in Python 3

#  I have imprted numpy as np previously
print("This is a 2x2 matrix calculator")
a = float(input("Enter the number of the first matrix element of matrix 1: "))
b = float(input("Enter the number of the second matrix element of matrix 1: "))
c = float(input("Enter the number of the third matrix element of matrix 1: "))
d = float(input("Enter the number of the fourth matrix element of matrix 1: "))

a1 = float(input("Enter the number of the first matrix element of matrix 2: "))
b1 = float(input("Enter the number of the second matrix element of matrix 2: "))
c1 = float(input("Enter the number of the third matrix element of matrix 2: "))
d1 = float(input("Enter the number of the fourth matrix element of matrix 2: "))

m = np.array([[a, b], [c, d]])
n = np.array([[a1, b1], [c1, d1]])
det1 = np.linalg.det(m)
det2 = np.linalg.det(n)
dot1 = np.vdot(m, n)
dot2 = np.vdot(n, m)
mat_product1 = np.matmul(m, n)
mat_product2 = np.matmul(n, m)
m_inv = np.linalg.inv(m)
n_inv = np.linalg.inv(n)
solve1 = np.dot(m_inv, n)
solve2 = np.dot(n_inv, m)

print("Matrix 1: ")
print(m)
print("Matrix 2: ")
print(n)

The code continues on (and the same for 3x3 matrices) but my problem comes in when I try this:

print("This is a 2x2 matrix calculator")
matrix1 = []
while True:
    element1 = float(input("Enter the matrix numbers for matrix 1, one by one: "))
    matrix1.append(element1)
    if len(matrix1) == 4:
        break
matrix2 = []
while True:
    element2 = float(input("Enter the matrix numbers for matrix 2, one by one: "))
    matrix2.append(element2)
    if len(matrix2) == 4:
        break
m = np.array([[matrix1[0], matrix1[1]], [matrix1[2], matrix1[3]])
n = np.array([[matrix2[0], matrix2[1]], [matrix2[2], matrix2[3]])
det1 = np.linalg.det(m)
det2 = np.linalg.det(n)
dot1 = np.vdot(m, n)
dot2 = np.vdot(n, m)
mat_product1 = np.matmul(m, n)
mat_product2 = np.matmul(n, m)
m_inv = np.linalg.inv(m)
n_inv = np.linalg.inv(n)
solve1 = np.dot(m_inv, n)
solve2 = np.dot(n_inv, m)

print("Matrix 1: ")
print(m)
print("Matrix 2: ")
print(n)

It says that matrix1 and matrix2 are unresolved variables: Unresolved reference

When I tried it in the Python Console, I got this message: TypeError message

Is there any way around this, or is it not possible to achieve what I'm trying?

It turns out that what I was trying has been correct all along, and my only mistake was forgetting to put a pair of square brackets. So, this:

m = np.array([matrix1[0], matrix1[1]], [matrix1[2], matrix1[3]])
n = np.array([matrix2[0], matrix2[1]], [matrix2[2], matrix2[3]])

becomes this:

m = np.array([[matrix1[0], matrix1[1]], [matrix1[2], matrix1[3]]])
n = np.array([[matrix2[0], matrix2[1]], [matrix2[2], matrix2[3]]])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM