[英]why doesnt this double loop work?
First of all, here's the code: 首先,下面是代码:
def check_sudoku(n):
d=len(n)
i=0
s=1
while i<d:
print "i=",i
while s<d:
print "s=",s
if n[0][i]==n[s][i]:
return False
s=s+1
i=i+1
return True
what I want to do is that after the value of s
changes from 1
to d
, then it loops again and the value of i
changes. 我想做的是,在
s
的值从1
变为d
之后,它再次循环并且i
的值改变。 But in my code the value of i
is not changing at all. 但是在我的代码中,
i
的值完全没有改变。
Just to be clear of what I want to do, say 只是说清楚我想做什么
n =[[1,2,3,4],
[2,3,1,3],
[3,1,2,3],
[4,4,4,4]]
i want the following to happen: 我希望发生以下情况:
first it should check for 首先,它应该检查
n[0][0]==n[1][0] n[0][0]==n[2][0] n[0][0]==n[3][0]
after that value of i
should increase by 1 之后
i
值应该增加1
then it should go like this: 那么它应该像这样:
n[0][1]==n[1][1] n[0][1]==n[2][1] n[0][1]==n[3][1]
after this value of i
will increase again and this same loop will run. i
这个值之后将再次增加,并且将运行相同的循环。
This is not happening and I am not sure why. 这没有发生,我不确定为什么。 Please tell me what changes I should make to make it run the way I want to.
请告诉我我应该进行哪些更改以使其按照自己的方式运行。
You don't reset s
in the outer loop, so the inner loop will only execute once. 您无需在外部循环中重置
s
,因此内部循环仅执行一次。 After that, s < d
will never be true again. 之后,
s < d
将不再为真。
Your inner loop increments s
until it is greater than d
, but you never set it back to 1
. 您的内部循环将
s
递增,直到它大于d
,但您再也不会将其设置回1
。 Also, with your current indentation, you also increment i
in the inner loop, so it'll reach d
inside the inner loop causing the outer loop to only be run once too. 同样,使用当前的缩进,您还可以在内循环中增加
i
,因此它将在内循环内部到达d
,从而导致外循环也仅运行一次。
So, if d
is 4, your values will go: 因此,如果
d
为4,则您的值将变为:
i = 0, s = 1
i = 1, s = 2
i = 2, s = 3
# exit inner loop
return True
You'd be much better off using for
loops using range()
, avoiding your mistakes altogether: 使用
range()
for
循环会更好,完全避免了您的错误:
def check_sudoku(n):
for i in range(len(n)):
for s in range(1, len(n)):
if n[0][i]==n[s][i]:
return False
return True
With range
the inner loop will always start at 1
, range through to len(n)
every time without explicit resets needed. 使用
range
,内部循环将始终从1
开始,每次每次都到达len(n)
而无需显式重置。
If you use the any()
function you can collapse the whole test into one line: 如果使用
any()
函数 ,则可以将整个测试折叠为一行:
def check_sudoku(n):
return not any(n[0][i] == n[s][i] for i in range(len(n)) for s in range(1, len(n)))
Further to unwind's answer, this is the modified code in a way that should work. 为了进一步解答,这是以一种可行的方式修改后的代码。 I've changed the whitespace in the code and the operators in a way which most Python programmers will consider more conventional.
我已经更改了代码和运算符中的空白,这是大多数Python程序员会认为更传统的方式。
def check_sudoku(n):
d = len(n)
i = 0
s = 1
while i < d:
print "i=",i
while s < d:
print "s=",s
if n[0][i] == n[s][i]:
return False
s += 1
i += 1
s = 1
return True
n =[[1,2,3,4],
[2,3,1,3],
[3,1,2,3],
[4,4,4,4]]
check_sudoku(n)
I think you should move the statement i=i+1 over the second loop follow as: 我认为您应该将语句i = i + 1移至第二个循环,如下所示:
def check_sudoku(n):
d=len(n)
i=0
s=1
while i<d:
print "i=",i
while s<d:
print "s=",s
if n[0][i]==n[s][i]:
return False
s=s+1
i=i+1
return True
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.