可能重复:
Python Leave Loop Early
所以,我在python中有一个循环,我需要它从另一个数字中取一个数字,直到它为负数,并且每次减去时都会有一条消息。问题是,如果变量符合第一个条件,它将通过适合条件的整个循环并每次打印消息。
sb = bosshp12 - final
if sb <= 0:
print( 'You need to do 1 second of damage to get a 4% Soulbound. ' )
sb - final
if sb <= 0:
print( 'You need to do 2 seconds of damage to get a 4% Soulbound. ' )
因此,如果第一个if语句中的数字已经是< 0
,那么它仍将通过。我希望循环在sb <= 0
答案 0 :(得分:0)
如果我做对了,所有代码都在while循环中,你可以使用break语句
sb = bosshp12 - final
if sb <= 0:
print( 'You need to do 1 second of damage to get a 4% Soulbound. ' )
break
sb - final
if sb <= 0:
print( 'You need to do 2 seconds of damage to get a 4% Soulbound. ' )
OH看来elif声明似乎也符合你的需要吗?
sb = bosshp12 - final
if sb <= 0:
print( 'You need to do 1 second of damage to get a 4% Soulbound. ' )
sb - final
elif sb <= 0:
print( 'You need to do 2 seconds of damage to get a 4% Soulbound. ' )
这样,如果第一次检查时sb&lt; = 0为True,则不会检查第二个。