Skip to content
Discussion options

You must be logged in to vote

Hi, @GustaFranz,

match/case is best thought of as a decision-making tool, not a replacement for all validation or error handling.

A simple way to think about it is:

  • if/elif → Check conditions (>, <, ranges, multiple expressions, etc.).
  • match/case → Pick an action based on the shape or exact value of data.
  • try/except → Handle operations that can genuinely fail (file access, type conversion, network requests, database calls, etc.).

For your menu example, match/case improves readability because you're selecting between a fixed set of known options:

option = input("Choose an option: ")

match option:
    case "1":
        create_record()
    case "2":
        update_record()
    case "3":
 …

Replies: 5 comments 5 replies

Comment options

You must be logged in to vote
1 reply
@GustaFranz
Comment options

Comment options

You must be logged in to vote
1 reply
@GustaFranz
Comment options

Comment options

You must be logged in to vote
1 reply
@GustaFranz
Comment options

Comment options

You must be logged in to vote
2 replies
@GustaFranz
Comment options

@code-with-bhumi
Comment options

Answer selected by GustaFranz

This comment was marked as spam.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question Ask and answer questions about GitHub features and usage Programming Help Discussions around programming languages, open source and software development Welcome 🎉 Used to greet and highlight first-time discussion participants. Welcome to the community! source:ui Discussions created via Community GitHub templates
6 participants