Is there a better way to find the value of multiple bool values? (python)
In one of my projects, I need to test the values of 4 bool variables. Each variable can have either bool value. My current solution to this is to make an if/elif tree using the and
statement to cover every possible combination of the 4 bools. To me, this seems needlessly complex. Is there a better way of testing the values?
FlaminHotValdez (437)
You could use nested for loops to test every possible combination and its values.
The best way to do this is using an array instead of four separate variables. Then, you could just iterate over the array, counting how many are true and how many are false.
Code for that:
Then you could do some string manipulation to print which bools are true.
Or if you just need to do some basic counting: