中途停止while循环-Python
在Python语句中间停止“while”循环的最佳方法是什么?我知道中途停止while循环-Python,python,python-3.x,variables,while-loop,break,Python,Python 3.x,Variables,While Loop,Break,在Python语句中间停止“while”循环的最佳方法是什么?我知道break,但我认为使用这种方法是不好的做法 例如,在下面的代码中,我只希望程序打印一次,而不是两次 variable = "" while variable == "" : print("Variable is blank.") # statement should break here... variable = "text" print("Variable is: " + variabl
break
,但我认为使用这种方法是不好的做法
例如,在下面的代码中,我只希望程序打印一次,而不是两次
variable = ""
while variable == "" :
print("Variable is blank.")
# statement should break here...
variable = "text"
print("Variable is: " + variable)
你能帮忙吗?提前感谢。break很好,尽管它通常有条件地使用。无条件使用时,它会提出一个问题,即为什么要使用
而循环:
# Don't do this
while condition:
<some code>
break
<some unreachable code>
# Do this
if condition:
<some code>
而不是
<some code>
while <some condition>:
<some code>
而:
用if
替换时的怎么样?为什么在这里使用break
是一种不好的做法?
while True:
<some code>
if <some condition>:
break
<some code>
while <some condition>:
<some code>