【发布时间】:2021-10-01 08:41:24
【问题描述】:
我想要做的是如果 num == 4 那么它之后的打印语句将不会运行,它将退出 while 循环
num = 3
def ifnum():
ifnum.loop = True
if num == 4:
ifnum.loop = False
print("num is 4")
ifnum()
while ifnum.loop == True:
num = int(input())
ifnum()
print("If num is 4 then i dont want this code to run")
print("same with this print statement")
【问题讨论】:
-
break将退出循环。 -
有没有办法将
break放入函数中? -
没有。
break只能在循环内使用 -
不,您希望该函数返回一个布尔值,然后您尝试使用
if语句。 -
sujay,我知道,哈哈,但是有没有可以在函数中使用的替代方法
标签: python python-3.x loops while-loop break