0

Hello can't wrap myself with this problem.I feel really stupid.

in this list of boolean the user can interact with it, and he is only allowed to have one True at the time. If one is True, the other four are False

scatter_is_01 = False
scatter_is_02 = False
scatter_is_03 = False
scatter_is_04 = False
scatter_is_05 = True
    if scatter_is_01==True: 
        scatter_is_02 = False
        scatter_is_03 = False
        scatter_is_04 = False
        scatter_is_05 = False

    if scatter_is_02==True:
        scatter_is_01 = False
        scatter_is_03 = False
        scatter_is_04 = False
        scatter_is_05 = False

    if scatter_is_03==True:
        scatter_is_02 = False
        scatter_is_01 = False
        scatter_is_04 = False
        scatter_is_05 = False

    if scatter_is_04==True:
        scatter_is_02 = False
        scatter_is_03 = False
        scatter_is_01 = False
        scatter_is_05 = False

This solution is stupid and is not even working properly. because the user is forced to set the value false again if he want to set another one true again.

could someone teach me ?

scatter_is_xx need to be boolean values because the api only use boolean to create toggle buttons.

DB3D
  • 186
  • 1
  • 12
  • 1
    You can probably model that better with an [`enum`](https://docs.python.org/3/library/enum.html) type. – jdehesa Sep 04 '19 at 16:54
  • 3
    An easier way would be to have a single integer variable whose value is 1-5 if one of them is true (with the value giving the number of the true one), or 0 if they are all false. Very simple, much less code. – Tom Karzes Sep 04 '19 at 16:54
  • 2
    Another option would be to use a list instead of separate variables. Any time you find yourself creating a bunch of variables with numeric suffixes, you though usually be using a list. – Barmar Sep 04 '19 at 16:56

3 Answers3

2

You've chosen an awkward representation for your information. If the flags can have exactly one item chosen at once, then simply represent that directly: a variable with a value in the range 1-5.

scatter = 4

expresses exactly the same information as your previous

scatter_is_04 = True
scatter_is_02 = False
scatter_is_03 = False
scatter_is_01 = False
scatter_is_05 = False

To check what used to be an individual flag:

if scatter_is_04:

is now

if scatter == 4:

After all, look at your variable name. The name you made is a wonderful description of exactly what information you need. Write it in a fashion more convenient to your usage.

Prune
  • 76,765
  • 14
  • 60
  • 81
0

You can tackle this in many ways, I'm sure - but an enum is a fairly elegant solution;

from enum import Enum

class Scatter(Enum):
    first = 1
    second = 2
    third = 3
    forth = 4
    fifth = 5

    @property
    def is_01(self):
        return self is Scatter.first

    @property
    def is_02(self):
        return self is Scatter.second

scatter = Scatter.first

print(scatter is Scatter.first)     # True
print(scatter is Scatter.second)    # False
print(scatter is Scatter.third)     # False

print(scatter.is_01)        # True
print(scatter.is_02)        # False
SteveJ
  • 3,034
  • 2
  • 27
  • 47
0

You could use a simple list to store values instead of the variables :

scatter_is_01 = False
scatter_is_02 = False
scatter_is_03 = False
scatter_is_04 = False
scatter_is_05 = True

# use a list instead of seperate variables

# Storing all the scatter variables in a list
scatter = [scatter_is_01,scatter_is_02,scatter_is_03,scatter_is_04,scatter_is_05] 
# you can also directly define the it as -
# scatter = [False,False,False,False,True]

def scatter_changer(index): # index starts from 0
    for i in range(len(scatter)):
        if( i == index ):
            scatter[i] = True
        else:
            scatter[i] = False 

scatter_changer(1)      # this sets scatter_is_02 to true and all others false
mr_pool_404
  • 488
  • 5
  • 16