循环也不会停止循环循环
在我的代码中,我必须向用户询问一些输入。满足条件时,程序应停止运行!
ALIVE = True
def you_died():
global ALIVE
ALIVE = False
def some_input():
choice = input()
if choice == "yes":
you_died()
while ALIVE is True:
some_input()
print("some string")
为什么我的代码仍在打印“一些字符串”,即使变量活着是错误的?
如何从功能内部打破循环?
In my code I have to ask users some input. When a condition is met the program should stop running!
ALIVE = True
def you_died():
global ALIVE
ALIVE = False
def some_input():
choice = input()
if choice == "yes":
you_died()
while ALIVE is True:
some_input()
print("some string")
Why is my code still printing "some string" even though the variable ALIVE is False?
How to break the loop from inside the function?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尽管每次迭代一次评估条件,因此更改条件中使用的变量不会导致其立即破裂。另外,您不能
break
从函数内部进行。但是,您可以在调用功能后测试全局并在执行其他循环中执行其他任何步骤之前打破函数:While conditions are evaluated once per iteration so changing the variable that is used in the condition won't cause it to immediately break. Also, you can't
break
from within a function. BUT you CAN test your global after calling your function and break if it isn't true before performing any other steps in your while loop:只有
break
,异常可以在执行的中间停止循环。否则,即使条件不再正确,当前的迭代也将完成:true
,因此开始第一迭代。some_input
修改Alive
,因此条件不再正确。但是,现在还不是时候检查情况了!print(stupt)
false
,因此停止循环。基本上,循环以与书面形式相同的方式执行:
true
,<代码> goto 2 。否则跳过整个循环(goto 4
)。break
语句,请跳出当前循环:goto 4
。goto 1
。Only
break
and exceptions can stop loops in the middle of execution. Otherwise, the current iteration will complete even if the condition is no longer true:True
, so begin 1st iteration.some_input
which modifiesALIVE
, so the condition isn't true anymore. However, it's not time to check the condition yet!print(stuff)
False
, so stop the loop.Basically, a loop is executed in the same manner that it's written:
True
,goto 2
. Otherwise skip the entire loop (goto 4
).break
statement, jump out of the current loop:goto 4
.goto 1
.您在调用 some_input()之后打印
,在循环重新启动之前和
Alive 决定不在调用Alive
时,都没有机会可以让循环进行some_input()
和执行print
之间不重复。一个简单的修复程序是在循环启动之前调用一次
some_input()
,以便Alive
在第一个some_input()。 >调用,然后在循环末尾(而不是开始)调用
some_input()
,以便在再次打印之前重新评估active
:一种更好的方法组织此代码将是让您的函数告诉您是否死亡(通过其返回值,而不是
global
),而break
立即循环(如果是)。这使您可以在true 时使用简单的,依靠
break
在适当的时间停止循环:You print
"some string"
after callingsome_input()
, before the loop restarts andALIVE
is checked, so there's no opportunity for the loop to decide not to repeat in between callingsome_input()
and doing theprint
.A simple fix would be to call
some_input()
once before your loop starts, so thatALIVE
is initialized with the result of the firstsome_input()
call, and then callsome_input()
at the end of the loop (not the beginning) so thatALIVE
is re-evaluated before you print again:A better way to organize this code would be to have your function tell you if you died (via its return value, not a
global
), andbreak
the loop immediately if so. This allows you to use a simplewhile True
, relying on thebreak
to stop the loop at the appropriate time:将
Alive
变量更改为false
,只会停止执行新迭代的语句。
如果要
print(“一些字符串”)
语句才能仅在用户回答了“是”以外的其他内容,对当前实现的更改很小,您可以将
print
移至some_input
喜欢:但是,我强烈建议您不要使用这种方法。原因如下:
全局变量:使用全局变量会导致隐藏的副作用。对于较大的程序,很容易丢失可能使用并更改您的
Alive
变量的所有代码片段。从长远来看,全球变量的过度使用将导致复杂性的增加。由于您已经定义了该函数,因此you_died
专门处理停止while/loop
语句的过程,您可能需要通过定义Alive 输入参数,将其更改为
false
并返回其新值。“是” vs是“是”:您是否希望您的程序在用户回答“是”或“是”,“是的”,甚至是“是!”吗?如果是这样,也许更改
如果选择==“是”:...
到之类更强大的方法。
考虑到两个上述要点,这是有关如何重新编写代码的建议:
Changing the
ALIVE
variable toFalse
, will only stop thewhile
statement of executing a new iteration.If you want the
print("some string")
statement to only execute when theuser answers something other than "yes", with minimal changes to your current implementation, you can move the
print
tosome_input
like so:However, I strongly advise you not to use this approach. Here's why:
Global variables: The use of global variables leads to hidden side effects. For larger programs, it's easy to lose track of all the pieces of code that might be using and altering your
ALIVE
variable. In the long run, overuse of global variable will lead to an increase in complexity. Since you've defined the function,you_died
to specifically handle the process of stopping yourwhile/loop
statement, you might want to maximize the pros of functions by defining analive
input parameter to it, changing it toFalse
and returning its new value."Yes" vs "yes": would you like your program to stop the
while
statement, when the user answers "yes", or "YES", "Yes", and even "Yes!"? If so, perhaps changingif choice == "yes":...
to something likeif "yes" in choice.lower():...
might be a more robust approach.Taking into account both aforementioned points, here's a suggestion on how you might want to re-write your code: