提问人:NONEenglisher 提问时间:12/15/2008 最后编辑:Karl KnechtelNONEenglisher 更新时间:1/13/2023 访问量:261609
如何停止 While 循环?[已结束]
How can I stop a While loop? [closed]
问:
我在一个函数中写了一个,但不知道如何停止它。当它不满足其最终条件时,循环就会永远持续下去。我怎样才能阻止它?while loop
def determine_period(universe_array):
period=0
tmp=universe_array
while True:
tmp=apply_rules(tmp)#aplly_rules is a another function
period+=1
if numpy.array_equal(tmp,universe_array) is True:
break #i want the loop to stop and return 0 if the
#period is bigger than 12
if period>12: #i wrote this line to stop it..but seems it
#doesnt work....help..
return 0
else:
return period
答:
25赞
Mapad
12/15/2008
#1
只需正确缩进代码即可:
def determine_period(universe_array):
period=0
tmp=universe_array
while True:
tmp=apply_rules(tmp)#aplly_rules is a another function
period+=1
if numpy.array_equal(tmp,universe_array) is True:
return period
if period>12: #i wrote this line to stop it..but seems its doesnt work....help..
return 0
else:
return period
您需要了解示例中的语句将退出您使用 创建的无限循环。因此,当中断条件为 True 时,程序将退出无限循环并继续到下一个缩进块。由于代码中没有 following 块,因此函数结束并且不返回任何内容。因此,我通过用语句替换语句来修复您的代码。break
while True
break
return
按照你的想法使用无限循环,这是最好的写法:
def determine_period(universe_array):
period=0
tmp=universe_array
while True:
tmp=apply_rules(tmp)#aplly_rules is a another function
period+=1
if numpy.array_equal(tmp,universe_array) is True:
break
if period>12: #i wrote this line to stop it..but seems its doesnt work....help..
period = 0
break
return period
评论
0赞
Mapad
12/15/2008
是的,因为你的代码中还有另一个错误:一个 break 语句,它阻止了你的函数返回一些东西。我现在删除了它。
0赞
NONEenglisher
12/15/2008
但它现在总是返回 0......不过非常感谢~~~你真好
1赞
nosklo
12/16/2008
不要检查 something() 是否为 True:直接检查对象,如 “if numpy.array_equal(tmp,universe_array):”
7赞
Joel Coehoorn
12/15/2008
#2
def determine_period(universe_array):
period=0
tmp=universe_array
while period<12:
tmp=apply_rules(tmp)#aplly_rules is a another function
if numpy.array_equal(tmp,universe_array) is True:
break
period+=1
return period
评论
1赞
annakata
12/15/2008
+1,但最好指出什么+为什么,“虽然是真的”eeek
0赞
Patrick Desjardins
12/15/2008
Joel 代码循环直到周期为 12 比停止循环和返回周期......这很正常...Joel 代码不是你想要的
0赞
Joel Coehoorn
12/15/2008
这更接近。是的,我确实希望他也能为自己考虑一些。
0赞
Joel Coehoorn
12/15/2008
请注意:如果 if 条件不为真,这仍然不会返回您想要的内容。我把最后一点留给读者作为练习。
0赞
NONEenglisher
12/15/2008
@Joel Coehoorn,我不能阻止自己说你真的是一个好老师!喜欢这个形式,因为它有很多像你这样的老师。
2赞
Greg Hewgill
12/16/2008
#3
Python 中的运算符可能不会按照您的期望执行。取而代之的是:is
if numpy.array_equal(tmp,universe_array) is True:
break
我会这样写:
if numpy.array_equal(tmp,universe_array):
break
运算符测试对象身份,这与相等性完全不同。is
-1赞
Suraj
12/17/2008
#4
我会使用 for 循环来做到这一点,如下所示:
def determine_period(universe_array):
tmp = universe_array
for period in xrange(1, 13):
tmp = apply_rules(tmp)
if numpy.array_equal(tmp, universe_array):
return period
return 0
-1赞
tkirk1222
6/28/2021
#5
以下是 Charles Severance 的“python or everybody”中关于在 True 循环中编写的一段示例代码:
while True:
line = input('> ')
if line == 'done':
break
print(line)
print('Done!')
这帮助我解决了我的问题!
评论
2赞
thehumaneraser
6/28/2021
嗨,@tkirk1222,欢迎来到 Stack Overflow!在回答问题时,请尽量保持你的回答尽可能笼统和切合主题,并提供更多的解释。在本例中,问题询问的是停止循环,因此您应该解释执行此操作的代码部分(语句)。其他东西可能会在没有解释的情况下误导其他人。while True
break
评论
IndentationError