如何在python三元组中使用Continue?那有可能吗?
例如
>>> for i in range(10):
... x = i if i == 5 else continue
给一个 SyntaxError: invalid syntax
如果可以进行三元继续,还有其他方法可以这样做:
>>> for i in range(10):
... if i ==5:
... x = i #actually i need to run a function given some condition(s)
... else:
... continue
...
>>> x
5
你不能; continue
是语句,而条件表达式是表达式,则不能在表达式内使用语句。毕竟,该continue
语句不会为要返回的条件表达式生成值。
改用一条if
语句:
if i == 5:
x = i
else:
continue
或更好:
if i != 5:
continue
x = i
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句