Python 3. oneline if elif else语句

时间:2015-06-01 12:50:12

标签: python python-3.x

我跑到这个我以前见过的新语法...... 而不是写:

foo = ""
if baa == 0: foo = "Heads"
else foo = "Tails"

我可以把整个事情写在一行:

foo = "Heads" if bar == 0 else "Tails"

但如果我想在2个以上的数值之间进行选择,这是实现目标的唯一途径吗?

foo = "Paper" if bar == 0 else "Rock" if bar == 1 else "Scissors"

或者有没有办法使用" elif"在某种程度上类似于:

#the following line does not work
foo = "Paper" if bar == 0 "Rock" elif bar == 1 else "Scissors"

PS。对不起,我是一个使用Python =>

的菜鸟

0 个答案:

没有答案
相关问题