我編寫了下面的代碼,我期待這樣,當第一個循環結束並且不返回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