'Is it possible to jump to default case in match statement?

I've seen people using goto statements in C-like languages (C, C++, C#, etc.) to jump to different cases in a switch statement, but as this is not possible in Python, I wondered if there is another way to achieve that in a match statement which was introduced in Python 3.10 as a soft-keyword.

number = input("Enter a two-digit number: ")
match len(number):
    case 2:
        try:
            number = int(number)
        except ValueError:
            # Jump to default case
        else:
            # Continue execution
    case _:
        print("You've entered an invalid number!")

The purpose is to print an error message in default case and jump to the default case when something is wrong in any other case, instead of rewriting the same error message, as can be seen in above example



Solution 1:[1]

In your case, I think the match statement isn't needed. You're looking for the input to consist of digits and to be a length of 2. You can rule out invalid input at the top of your code and then continue your logic with the else statement.

if not (number.isdigit() and len(number) == 2)):
    print("You've entered an invalid number!")
else:
    number = int(number)
    # Continue execution

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Richard Dodson