我编写了下面的代码,我期待这样,当第一个循环结束并且不返回False时,流程会在第二个while循环之后进行。但是,流程会跳过第二个while循环并简单地返回True。这是为什么?我怎样才能解决这个问题,使第一个while循环转到第二个while循环之后的流程?为什么在Python中两个while循环(不在另一个循环中)不起作用?
square = [[1,2,3,4],[4,3,1,4],[3,1,2,4],[2,4,4,3]]
# this is an auxiliary function
def getSum(lis):
sum = 0
for e in lis:
sum = sum + e
return sum
# here is where the problem is
def check_game(square):
standardSum = getSum(range(1, len(square)+1))
while square: #this is the first while loop
row = square.pop()
print row, 'row', 'sum of row=', getSum(row)
if standardSum != getSum(row):
return False
m = 0
while m < len(square): # the second while loop, which the flow skips
n = 0
col = []
while n < len(square):
col.append(square[n][m])
n = n + 1
print col, 'column'
if standardSum != getSum(col):
print standardSum, ' and sum of col =', getSum(col)
return False
m = m + 1
return True
检查压痕这里符合您的实际代码 – 2012-03-11 17:51:48
+1 @AramKocharyan:既不的while循环实际上是check_game功能可按内部。 – 2012-03-11 18:02:32
是的,我粘贴错了,但在我的.py文件中它是正确的缩进。谢谢,亚兰。 – craftApprentice 2012-03-11 18:08:37