Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
assigning index
#1
Basicly I have a certain amount of positions (1-3) which can be either True or False.

I want the function I use to get the decent index value.

So
pos1 T
pos2 F
pos3 T
gives indexes 0 and 1

while example below gives 0, 1, 2

and
pos1 T
pos2 F
pos3 F
gives index 0

To me my code feels a bit strange. Lack of knowledge for sure, so what would you suggest?

triggers = [100,200,300]

pos1 = True #never changes
pos2 = True
pos3 = True

Somevalue = 3

pos1 = triggers[0]
if pos2:
    customfunction(a, b, triggers[1])
    if pos3:
        customfunction(a, c,triggers[2])
elif pos3:
	customfunction(a, c, triggers[1])
Reply
#2
I don't understand the problem or the goal.
what is somevalue supposed to do? It's never used
Reply
#3
I also am not sure what you need help with. And the code is confusing. The comment on line3, for example, states that pos1 never changes, but then you immediately change it on line 9.
Reply
#4
Code: yeah explaining is difficult (can paste the whole code but it's a mixed with specific programming coding which makes things more unclear while the case is easier)

Trying a different explaination approach:
pos1 pos2 pos3 can be
1. pos1, pos2, pos3
2. pos1, pos2
3. pos1, pos3
each possibility triggers another value in a customfunction (hence the b, c, d value)
BUT I want things nice and tidy.
so call them first place, second place, and third place if you like:
1. first, second, third
2. first, second
3. first, second

My quesiton is about 2. and 3.: how to ensure that a T/F decisions triggers the correct places?

Makes more sense?
I read my coding as follows:
"pos1 is already given, and is executed. if pos2 is True, assign it to position 2 and while I'm at it: if pos3 is true: assign that to the third place. IF pos2 isn't true, and pos3 is True, assign position 2 to pos3."

or perhaps even easier: 3 runners participate in a competition. they can end up 1, 2, 3. If 2 backs out: number 3 becomes second not 3th. How to achieve that?

I thought using a while function (I can get the amount of participants) BUT the customfunction triggers not only that but also another value (b, c, d) ...

Horrible at explaining stuff. Sorry.


triggers = [100,200,300]
 
pos1 = True #never changes
pos2 = True
pos3 = True
 
if pos1:
    customfunction(a, d, triggers[0])
if pos2:
    customfunction(a, b, triggers[1])
    if pos3:
        customfunction(a, c,triggers[2])
elif pos3:
    customfunction(a, c, triggers[1])
Reply
#5
What about a dict, where the keys are tuples of 3 bools, and the value is the expected action to take? Something like...
>>> conditions = {
... (True, True, True): [1, 2, 3],
... (True, True, False): [1, 2],
... (True, False, True): [1, 4],
... (True, False, False): [1],
... (False, True, True): [2, 3],
... (False, True, False): [2],
... (False, False, True): [4]
... }
>>> def customfunction(left, right, trigger):
...   print(f"Running with: {left}:{right} => {trigger}")
...
>>> triggers = [100, 200, 300]
>>> pos1 = True
>>> pos2 = True
>>> pos3 = True
>>> op_map = {
... 1: lambda a, b, c, d: customfunction(a, d, triggers[0]),
... 2: lambda a, b, c, d: customfunction(a, b, triggers[1]),
... 3: lambda a, b, c, d: customfunction(a, c, triggers[2]),
... 4: lambda a, b, c, d: customfunction(a, c, triggers[1])
... }
>>> operations = conditions[(pos1, pos2, pos3)]
>>> for op in operations:
...   op_map[op](1, 2, 3, 4)
...
Running with: 1:4 => 100
Running with: 1:2 => 200
Running with: 1:3 => 300
Reply
#6
Okay @nilamo,

Thanks for your reply. Tiny side note: I'm rather new to python so getting "overwhelmed" by your suggestion eventhough that's kinda what I asked for. So I'll dive into it!

So first reaction is: a dictionary is the 'best' way to deal with situations like this? So for example my case: limited to only a few possibilities up to a case of 100's of possibilities?

My suggested code for my limited case would be a no-go?
Or is yours just way more bullet proof?

(again: not questioning your expertise, just want to learn!)
Reply
#7
I don't think that's the best. My example, for instance, gives errors if all three are False.

But I do think that some variation of the "command" pattern would work very well here. A good explanation of the pattern can be found here: http://gameprogrammingpatterns.com/command.html
That link is part of a book of game design patterns, but a lot of what's in there applies to general programming as well. Also, the code is c++, but the explanations are what you should be worried about, not the code.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Assigning a new value to variable uriel 1 1,568 Dec-04-2021, 02:59 PM
Last Post: Underscore
  assigning a variable :( gr3yali3n 0 1,289 Sep-22-2020, 09:02 PM
Last Post: gr3yali3n
  Assigning variables Godserena 4 2,133 Apr-26-2020, 06:59 AM
Last Post: buran
  Assigning a Variable Help MC2020 5 2,880 Jan-06-2020, 10:54 PM
Last Post: MC2020
  Assigning an item from a list xlev 1 1,429 Sep-27-2019, 04:42 PM
Last Post: Larz60+
  Not adding and assigning? 00712411 7 4,061 Oct-10-2018, 07:11 PM
Last Post: volcano63
  Assigning iter_row value to variable ankey 8 8,945 Sep-24-2018, 03:51 PM
Last Post: ankey
  Assigning a new variable in a IF loop pythoneer 5 3,735 Mar-02-2018, 05:21 AM
Last Post: pythoneer
  Assigning to string slice michaeljhuman 1 2,727 Feb-08-2018, 12:57 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020