我跑到这个我以前见过的新语法...... 而不是写:
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 =>
的菜鸟