-1

I want to run one of these functions when a certain strike is reached.

draw_order = [draw_head(), draw_body(), draw_bothlegs(), draw_botharms()]

def strikecount(strikenum):

    global strikes
    strikes = strikes + 1

    if strikenum == 1:
        draw_order(strikes)

    if strikenum == 2:
        draw_order(strikes)

    if strikenum == 3:
        draw_order(strikes)

    if strikenum == 4:
        draw_order(strikes)

I tried using the indexes but was unsure on how to do it.

Axe319
  • 4,255
  • 3
  • 15
  • 31
  • 3
    What's the point of the four conditionals if exactly the same happens in each one of them? – gspr Mar 13 '23 at 19:48
  • I wanted something different to happen in each of them, so I used indexes to try and get a different function – Christopher Chang Mar 13 '23 at 19:49
  • 6
    `draw_order = [draw_head(), draw_body(), draw_bothlegs(), draw_botharms()]` this is *not* creating a `list` with 4 functions. It's creating a `list` with the `return` value of 4 functions. You most likely want to remove the parentheses `()` to assign the function objects themselves. – Axe319 Mar 13 '23 at 19:50
  • @gspr `draw_order` is a list which contains functions. OP needs to access a particular function in `draw_order` for each condition. – wjandrea Mar 13 '23 at 19:50
  • @gspr instead of putting an integer in the place, I created a strike variable that increases everytime I iterate the function. – Christopher Chang Mar 13 '23 at 19:52
  • 1
    To @gspr's point, calling `strikecount()` with parameters `1`, `2`, `3`, or `4`. produces the exact same result, so what is the point? At any rate, this feels like an XY Problem. Why have you taken such a convoluted path to calling these functions? What are you REALLY trying to solve by writing code like this? – JNevill Mar 13 '23 at 19:55
  • @ChristopherChang Using a global like this probably isn't the best approach. Globals should generally be avoided. [See this for more info](https://stackoverflow.com/a/19158418/11044046). It'd probably be more effective to pass the value to the function or add it as an a attribute to an object. – nlivingstone Mar 13 '23 at 19:56
  • I'm making a big assumption here, but this feels like maybe you want a "state machine". Iterating through a function and performing different actions based on the iteration (or other inputs). I think you just went a little to abstract with this list-of-functions approach. You are wading dangerously near `eval()` territory and that, to me at least, is a code smell. – JNevill Mar 13 '23 at 20:05
  • BTW, welcome to Stack Overflow! Check out the [tour], and [ask] for tips like starting with your own research. I found the linked questions by googling `python put functions in a list and call them later`. – wjandrea Mar 13 '23 at 20:16

1 Answers1

0

You can index them directly from the list and call them as follows. This is assuming that the draw functions are already defined.

draw_order = [draw_head, draw_body, draw_bothlegs, draw_botharms]

def strikecount(strikenum):

    global strikes
    strikes = strikes + 1

    draw_order[strikenum-1](strikes)
  • It's worth noting that indexing starts at 0, not 1. So whatever code passes in `strikenum` will need to be adjusted. – wjandrea Mar 13 '23 at 20:05