Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么Python中的两个while循环一个接一个(而不是在另一个内部)don';不行?_Python_While Loop_Control Flow - Fatal编程技术网

为什么Python中的两个while循环一个接一个(而不是在另一个内部)don';不行?

为什么Python中的两个while循环一个接一个(而不是在另一个内部)don';不行?,python,while-loop,control-flow,Python,While Loop,Control Flow,我写了下面的代码,我希望,当第一个循环结束并且没有返回False时,流将跟随第二个while循环。但是,该流跳过第二个while循环,只返回True。为什么呢?如何解决此问题,使第一个while循环之后的流转到第二个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:

我写了下面的代码,我希望,当第一个循环结束并且没有返回False时,流将跟随第二个while循环。但是,该流跳过第二个while循环,只返回True。为什么呢?如何解决此问题,使第一个while循环之后的流转到第二个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 
square=[[1,2,3,4],[4,3,1,4],[3,1,2,4],[2,4,4,3]]
#这是一个辅助功能
def getSum(lis):
总和=0
对于lis中的e:
sum=sum+e
回报金额
#这就是问题所在
def检查_游戏(方形):
standardSum=getSum(范围(1,长度(平方)+1))
while square:#这是第一个while循环
row=square.pop()
打印行'row','sum of row=',getSum(row)
如果是标准金额!=getSum(行):
返回错误
m=0
whilem转置
只是一个技巧。