Mira Wadu
Mira Wadu

Reputation: 53

How to go back to first if statement if two matrices are not equivalent?

I have a if/else statement like this:

import numpy as np
rows_b = int(input("Enter the number of rows of matrix B : " ))
column_b = int(input("Enter the number of columns of matrix B : "))

print("Input elements of matrix B1:")
B1= [[float(input()) for i in range(column_b)] for j in range(rows_b)]
   
print("Input elements of matrix B2:")
B2= [[float(input()) for i in range(column_b)] for j in range(rows_b)]

b1 = np.array(B1)
b2 = np.array(B2)

result = np.all(b1 == b2[0])
if result:
    print('matrix B1 = B2')
else:
    print('matrix B1 and B2 are not equivalent') 
    
B = np.array(B1)
print("Matrix B is: ") 
for x in B:
    print(x)

I want if B1 = B2 then continue to the next step (B = np.array (B1)) but (else) if B1 and B2 are not equal then go back to the first statement, how ?

Upvotes: 0

Views: 34

Answers (1)

Muhammad Adeel
Muhammad Adeel

Reputation: 175

you can make function and call it recursively. Following is the code:-

def check_matrices():
    rows_b = int(input("Enter the number of rows of matrix B : " ))
    column_b = int(input("Enter the number of columns of matrix B : "))
    
    print("Input elements of matrix B1:")
    B1= [[float(input()) for i in range(column_b)] for j in range(rows_b)]

    print("Input elements of matrix B2:")
    B2= [[float(input()) for i in range(column_b)] for j in range(rows_b)]

    b1 = np.array(B1)
    b2 = np.array(B2)
    
    result = np.all(b1 == b2[0])
    
    if result:
        print('matrix B1 = B2')
    else:
        check_matrices()
    B = np.array(B1)
    print("Matrix B is: ") 
    for x in B:
        print(x)

        
check_matrices()

Upvotes: 1

Related Questions