Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Have extra while loop conditions ... based on a condition?

The variable a can take any number of values. The value of a is the amount of extra pre-defined conditions to have for the while loop.

This can be done with multiple elif statements but is there a cleaner way of doing this?

if a == 0:
    while condition_1:
        ...
elif a == 1:
    while condition_1 or condition_2:
        ...
elif a == 2:
    while condition_1 or condition_2 or condition_3:
        ...
like image 201
Lobstw Avatar asked Mar 16 '16 01:03

Lobstw


People also ask

Can you have 2 conditions in a while loop?

Using multiple conditions As seen on line 4 the while loop has two conditions, one using the AND operator and the other using the OR operator. Note: The AND condition must be fulfilled for the loop to run. However, if either of the conditions on the OR side of the operator returns true , the loop will run.

Can you have multiple conditions in a while loop JavaScript?

The general syntax of a while loop in JavaScript is given below. Here, condition is a statement. It can consist of multiple sub-statements i.e. there can be multiple conditions. For as long as condition evaluates to true , the code written inside the while block keeps on executing.

Can you have multiple If statements in a while loop Python?

Python compares the two values and if the expression evaluates to true, it executes the loop body. However, it's possible to combine multiple conditional expressions in a while loop as well.

Can a while loop have two conditions in C?

Yes. you can put multiple conditions using “&&”, ”||” in while loop.


2 Answers

A general way of doing what other languages do with a switch statement is to create a dictionary containing a function for each of your cases:

conds = {
    0: lambda: condition_1,
    1: lambda: condition_1 or condition_2,
    2: lambda: condition_1 or condition_2 or condition_3
}

Then:

while conds[a]():
    # do stuff

By using lambdas (or named functions if your conditions are particularly complex) the appropriate condition can be evaluated each time through the loop, instead of once when the dictionary is defined.

In this simple case where your a has sequential integer values starting at 0, you could use a list and save a bit of typing. To further simplify, you could define each of your conditions in terms of the previous one, since you're just adding a condition each time:

conds = [
     lambda: condition_1,
     lambda: conds[0]() or condition_2,
     lambda: conds[1]() or condition_3
]

Or, as suggested by Julien in a comment:

conds = [
     lambda: condition_1,
     lambda: condition_2,
     lambda: condition_3
]

while any(cond() for cond in conds[:a+1]):
    # do stuff
like image 191
kindall Avatar answered Nov 14 '22 23:11

kindall


Have you tried something like this:

while (a >= 0 and condition_1) or (a >= 1 and condition_2) or (a >= 2 and condition_3) ...
like image 23
Blundering Philosopher Avatar answered Nov 14 '22 21:11

Blundering Philosopher