有人能帮我把for loop替换成,而loop我正在努力解决它吗
这个问题特别要求我们不要使用for循环。这就是为什么我需要用while循环替换它
我列举了以下几点:
def matrix_equal(matrix1, matrix2):
"""
-------------------------------------------------------
Compares two matrices to see if they are equal - i.e. have the
same contents in the same locations.
Use: equal = matrix_equal(matrix1, matrix2)
-------------------------------------------------------
Parameters:
matrix1 - the first matrix (2D list of ?)
matrix2 - the second matrix (2D list of ?)
Returns:
equal - True if matrix1 and matrix2 are equal,
False otherwise (boolean)
------------------------------------------------------
"""
equal = True
if((len(matrix1) != len(matrix2)) or (len(matrix1[0]) != len(matrix2[0]))):
equal = False
for x in range(len(matrix1)):
if(equal == False):
break
for y in range(len(matrix1[0])):
num1 = matrix1[x][y]
num2 = matrix2[x][y]
if(num1 != num2):
equal = False
break
return equal
样本测试:
First matrix:
0 1 2
0 c a t
1 d o g
2 b i g
Second matrix:
0 1 2
0 c a t
1 d o g
2 b i g
Equal matrices: True
我们必须遵循的条件:
1. should not call input in the function
2. should not call print in the function
3. should not have multiple returns
改变
到
干杯
您可以转换:
进入
所以你会得到:
测试一下
这将解决您的问题,这是一个使用while循环的解决方案:
相关问题 更多 >
编程相关推荐