【问题标题】:Python match case(switch) invalid syntax? [duplicate]Python匹配大小写(开关)无效语法? [复制]
【发布时间】:2022-01-09 21:16:25
【问题描述】:

使用 python 3.7.3。 所有文档都指向这是在 python 中执行 switch 语句的正确方法。不知道为什么它不起作用。我应该导入匹配吗? 下面有语法错误。

    match player:
               ^
def evaluate(player, ai):
    match player:
        case Choice.rock:
            if ai == Choice.rock:
                return "draw"
            elif ai == Choice.scissors:
                return "player wins"
            elif ai == Choice.paper:
                return "ai wins"
        case Choice.paper:
            if ai == Choice.rock:
                return "player wins"
            elif ai == Choice.scissors:
                return "ai wins"
            elif ai == Choice.paper:
                return "draw" 
        case Choice.scissors:
            if ai == Choice.rock:
                return "ai wins"
            elif ai == Choice.scissors:
                return "draw"
            elif ai == Choice.paper:
                return "player wins"

【问题讨论】:

  • match 在 python 3.10 中添加

标签: python python-3.x


【解决方案1】:

match 需要 python 版本 >= 3.10,见what's new

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-07-02
    • 1970-01-01
    • 2016-07-30
    • 2023-01-06
    • 1970-01-01
    • 2010-11-08
    • 2022-12-03
    相关资源
    最近更新 更多