Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

216
Visualizações
SyntaxError: invalid syntax when using match case

I've been trying to use a match case instead of a million IF statements, but anything I try returns the error:

    match http_code:
          ^
SyntaxError: invalid syntax

I've also tried testing examples I've found, which also return this error, including this one:

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")

I'm aware that match cases are quite new to python, but I'm using 3.10 so I'm not sure why they always return this error.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Two possibilities: you've got a syntax error on an earlier line (count your opening ( and closing )!), or you're not using Python 3.10

As a commenter said, check your Python version: import sys;print(sys.version)

Here are my (as-expected) results running in 3.10:


Python 3.10.0 (tags/v3.10.0:b494f59, Oct  4 2021, 19:00:18) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> 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")
   
Out[1]: "I'm a teapot"
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda