Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Fastest/ most efficient way to trigger an if statement if specific numbers are met

There are multiple ways to do this, but I’m looking for the fastest/ most efficient way.

I’ve got a counter, starting at 0, which increments every time an event triggers and goes up to 1000.
If the counter gets to 101, 201, 301, 401, 501, 601, 701, 801 or 901, something needs to happen.

Most simple way:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

        if counter == 101:
            Do something
        if counter == 201:
            Do something
        if counter == 301:
            Do something
        if counter == 401:
            Do something
        #etc .......

We could make it more efficient by the following:

    if counter < 501:
        if counter == 101:
            Do something
        if counter == 201:
            Do something
        #etc .......
    if counter >= 501:
        if counter == 501:
            Do something
        if counter == 601:
            Do something
        #etc .......

Or even putting it in a loop in order to add a break:

for i in range (1):
    if counter < 501:
        if counter == 101:
            Do something
            break
        if counter == 201:
            Do something
            break
        #etc .......
    if counter >= 501
        if counter == 501:
            Do something
            break
        if counter == 601:
            Do something
            break
        if counter == 701:
            Do something
        #etc .......

BUT, there problem is that it’s always going to trigger when the counter is in between the numbers (1, 2, 3, 102, 105, 106, 450, 459, 891, etc).

Another way I was thinking of was as follows:

if (counter == 101) OR (counter == 201) OR (counter == 301): #etc
    Do something according to number

But that’s basically the same as having a bunch of if statements like the first example.

Lastly, I was thinking about it only triggering if the counter = like x01 (so x can be any number followed by 01).

for i in (0):
if "01" in str(counter):
    if counter == 101:
        do something
        break
    if counter == 201: 
        do something
        break

Wondering what you guys think

*EDIT: some are asking what "do something" is. It’s simply changing a variable. So if counter = 101 -> var = "hello", if counter = 201 -> var = "bye", etc..

>Solution :

With the initial question the response from gchapuis would have been good. Now, since you actually only want to set a variable value, you can keep the dictionary approach but remove the "callable" idea. With this in mind, you could do the following:

MAPPINGS = {
    101: "hello",
    201: "bye"
}

# later in your code

var = MAPPINGS.get(counter, var)  # give var as default, so it's unchanged if the counter value is not in the dict
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading