结束一个循环,蟒蛇早期
问题描述:
所以,我有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. ')
因此,如果该号码已经是< 0
在第一if语句,它仍然会通过。我希望循环后sb <= 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. ')
这样,如果某人< = 0是在所述第一检查真,第二个将不被检查。
+0
如果我选中第一个,则表示意外缩进。它还表示elif的语法无效。 – user1349560 2012-04-22 13:58:09
+1
你正在使用'elif'具有相同的条件? – 2012-04-22 14:13:35
我没有在你的例子中看到任何循环... – georg 2012-04-22 13:40:51
没有循环也没有'sb - 最终'什么都不做,你想'sb = sb -final'或缩短:'sb - =最终' – jamylak 2012-04-22 13:41:54
我讨厌是那个人,但你真的应该[阅读教程](http://docs.python.org/tutorial/controlflow.html#break-and-continue-statements-and-else-clauses-on-loops)。 – senderle 2012-04-22 13:41:59