小编Jav*_*Cry的帖子

SyntaxError:使用匹配大小写时语法无效

我一直在尝试使用匹配大小写而不是一百万个 IF 语句,但我尝试的任何操作都会返回错误:

    match http_code:
          ^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)

我还尝试过测试我发现的示例,这些示例也会返回此错误,包括以下错误:

http_code = "418"

match http_code:
    case "200":
        print("OK")

    case "404":
        print("Not Found")

    case "418":
        print("I'm a teapot")

    case _:
        print("Code not found")
Run Code Online (Sandbox Code Playgroud)

我知道匹配案例对于 python 来说是相当新的,但我使用的是 3.10,所以我不确定为什么它们总是返回这个错误。

python pattern-matching

43
推荐指数
1
解决办法
11万
查看次数

标签 统计

pattern-matching ×1

python ×1