一尘不染

case / switch语句的Python等效项是什么?

python

我想知道,是否有Python等效的case语句,例如VB.net或C#上可用的示例?


阅读 466

收藏
2020-02-23

共1个答案

一尘不染

例如:

# define the function blocks
def zero():
    print "You typed zero.\n"

def sqr():
    print "n is a perfect square\n"

def even():
    print "n is an even number\n"

def prime():
    print "n is a prime number\n"

# map the inputs to the function blocks
options = {0 : zero,
           1 : sqr,
           4 : sqr,
           9 : sqr,
           2 : even,
           3 : prime,
           5 : prime,
           7 : prime,
}

然后,调用等效的开关块:

options[num]()

如果你严重依赖失败,就会开始崩溃。

2020-02-23