Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7983289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T10:55:16+00:00 2026-06-04T10:55:16+00:00

What I have found so far in many cases python code for checking business

  • 0

What I have found so far in many cases python code for checking business logic or any other logic is some thing like below(simplified):

user_input = 100
if user_input == 100:
    do_something()
elif user_input > 100:
    do_sth_different()
else:
    do_correct()

When a new logic need to be checked what new python programmer(like me) do just add a new bock in elif…

What is pythonic way to check a bunch of logic without using a long chunk of if else checking?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T10:55:17+00:00Added an answer on June 4, 2026 at 10:55 am

    The most common way is just a line of elifs, and there’s nothing really wrong with that, in fact, the documentation says to use elifs as a replacement for switches. However, another really popular way is to create a dictionary of functions:

    functions = {100:do_something,101:do_sth_different}
    user_input = 100
    try:
        functions[user_input]()
    except KeyError:
        do_correct()
    

    This doesn’t allow for your given if user_input > 100 line, but if you just have to check equality relationships and a generic case, it works out nicely, especially if you need to do it more than once.

    The try except case could be replaced by explicitly calling get on the dictionary, using your generic function as the default parameter:

    functions.get(user_input,do_correct)()
    

    If that floats your boat.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello this is may first question and I have found so far the following
I'm writing an equirectangular map viewer, all the stuff I have found so far
I have been looking for a SSO system. So far I have found CAS.
I have searched far and wide on the Internet but have not found anything
I have found the way to copy the record that I would like, but
I'm using NetBeans as my IDE. Whenever I have some code that uses another
I currently have a shell script that process many images one after the other,
I have found myself designing a language for fun that is a cross between
I have found numerous examples on uploading a file to a web server via
I have found a Tutorial here on how to implement drag and drop in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.