Complex switch statement

This question already has an answer here:

  • Replacements for switch statement in Python? 44 answers

  • 使用elif函数

    if x == 1:
        print ("case 1")
    elif x == 1 or x == 2:
        print ("case 2")
    elif x == 3:
      print ("3")
    elif x == 4 or x == 5:
      print ("4")
      print ("5")
    else:
      print ("default")
    

    You can use elif instead of writing

    else: 
        if x == 3:
    

    And in Python, if - elif is used as switch-case like below in the rewritten code of yours.

    def run(): 
        x = input() 
    if x == 1: 
        print ("case 1") 
    elif x == 2: 
        print ("case 2") 
    elif x == 3: 
        print ("3") 
    elif x == 4 or x == 5: 
        print ("4") 
        print ("5") 
    else: 
        print ("default")
    

    And for dictionary use please check this link: Replacements for switch statement in Python

    链接地址: http://www.djcxy.com/p/42774.html

    上一篇: 你如何在Python中使用省略号切片语法?

    下一篇: 复杂的switch语句