Skip to content
Advertisement

elif: SyntaxError: invalid syntax

I want to re.search within an if statement but regardless of identation, get syntax error. Is it because elif: has no condition?

JavaScript

error message

JavaScript

Advertisement

Answer

In elif you have if, it requires a condition and you provide none, it should be

JavaScript

Using walrus operator (since py3.8) you can improve your code to look like

JavaScript

If the code is executed multiple times, it should be better to compile the regex onces

Define the compiled regex globally in the file

JavaScript

Then use it in your methods

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement