我有一个if/else语句,像这样:
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')
#if matrix B1 = B2, go to the next algorithm
else:
print('matrix B1 and B2 are not equivalent')
#if B1 and B2 are not equivalent, stop here.
B = np.array(B1)
print("Matrix B is: ")
for x in B:
print(x)
我想如果B1 = B2,那么继续下一步(B = np.array ( B1 ))但是(else)如果B1和B2不相等,那么停止算法(不继续到B= np.array (B1)),如何?
发布于 2021-08-04 04:31:03
将其放入if
中
if B1 == B2:
B = np.array(B1)
print("Matrix B is: ")
for x in B:
print(x)
else:
print('matrix B1 and B2 are not equivalent')
#if B1 and B2 are not equivalent, stop here.
https://stackoverflow.com/questions/68651264
复制相似问题