首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >如果"statement if“为真,如何停止算法?

如果"statement if“为真,如何停止算法?
EN

Stack Overflow用户
提问于 2021-08-04 04:28:01
回答 1查看 85关注 0票数 2

我有一个if/else语句,像这样:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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)),如何?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-08-04 04:31:03

将其放入if

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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.
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68651264

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文