【问题标题】:Adding more than one line per case in a switch-case statement (python)在 switch-case 语句中每个案例添加多于一行(python)
【发布时间】:2020-03-15 17:46:39
【问题描述】:

我是编程新手,并试图了解如何在 python 中使用 switch-case 语句。我的问题是当我尝试使用每个 case 包含多于一行的语句时发生语法错误。这可能吗?我错过了什么?如果不可能,switch-case 语句有什么用处?

这是我用来测试的代码

drop = random.randint(1,3)
inventory = []
def randomDrop(i):
    switcher ={
        1:
            "you got x"
            inventory.append(x)   #syntax error on this line         
        2: 
            "you got y",
            inventory.append(y)

        3: 
            "you got z",
            inventory.append(z)

    }
    return switcher.get(i,"you got nothing")
randomDrop(drop)

【问题讨论】:

  • python 不支持switch。您使用的是字典,它是保存数据或引用的数据结构。请改用if-else

标签: python switch-statement syntax-error


【解决方案1】:

Python 不支持切换大小写。您正在使用的是字典。它是一个键值数据结构。 字典的语法:{key1:value1, key2:value2} 但是代替 value 您使用的是多个语句,这就是语法错误的原因。

drop = random.randint(1,3)
inventory = []
def randomDrop(i):
    switcher ={
        1:
            "you got x"
            inventory.append(x)   #multiple statement instead of value or reference        
        2: 
            "you got y",
            inventory.append(y)   #multiple statement instead of value or reference        

        3: 
            "you got z",
            inventory.append(z)   #multiple statement instead of value or reference        

    }
    return switcher.get(i,"you got nothing")
randomDrop(drop)

请改用if-else

inventory = []
def randomDrop(i):
    if i == 1:
        inventory.append(x)
        return 'you got x'
    elif i == 2:
        inventory.append(y)
        return 'you got y'
    elif i == 3:
        inventory.append(z)
        return 'you got z'
    else:
        return 'you got nothing'

drop = random.randint(1,3)
randomDrop(drop)

见: https://docs.python.org/3/tutorial/controlflow.html

【讨论】:

    【解决方案2】:

    我相信您正在尝试这样做。如果有帮助,请告诉我。

    inventory = []
    def randomDrop(i):
        if i == 1:
            inventory.append(x)
            print('you got x')
        elif i == 2:
            inventory.append(y)
            print('you got y')
        elif i == 3:
            inventory.append(z)
            print('you got z')
        else:
            print('you got nothing')
    
    drop = random.randint(1,3)
    randomDrop(drop)
    

    【讨论】:

    • 这就是我想要做的,只需要一个 switch 语句。我不知道 Python 不支持它们。
    【解决方案3】:

    Python 不支持切换大小写。您将不得不使用 if-elif-else insetad of switch case。

    【讨论】:

      【解决方案4】:

      要获得更可口和可维护的结构,您可以为 switch 使用辅助函数:

      def switch(v): yield lambda *c: v in c
      

      这将让您以类似 C 的风格编写代码:

      for case in switch(i): 
          if case(1):
              inventory.append(x)         
              return "you got x"
          if case(2): 
              inventory.append(y)
              return "you got y"
          if case(3):             
              inventory.append(z)   
              return "you got z"
      else:
          return "you got nothing"
      

      【讨论】:

        猜你喜欢
        • 2015-07-16
        • 1970-01-01
        • 1970-01-01
        • 2015-06-21
        • 1970-01-01
        • 2021-07-30
        • 2021-02-19
        • 2021-06-13
        • 2014-01-03
        相关资源
        最近更新 更多