0

I would like to put the returned player who sent the message !player1 or !player2 into a short list so that I can them for later, but I can't seem to find out how to get returned variables from functions inside of functions. I saw something about Closure which I think this is but even following the example I can't seem to get the returned values so I can assign them.

    def players(sender, event):

    def getplayer():

        msg = event.parsed[0]['msg']

        if msg == '!player1':
            BotBasicFunction.sendmsg(msgg='{} has joined the game!'.format(hasplayer()))
            hasplayer()

        if msg == '!player2':
            BotBasicFunction.sendmsg(msgg='{} has joined the game!'.format(hasplayer()))
            hasplayer()

    def hasplayer():
        invoker = event.parsed[0]['invokername']
        return invoker

    parsedmsg = event.parsed[0]['msg']

    players = [getplayer(), getplayer(), getplayer(), getplayer()]

    if parsedmsg == '!players':
        BotBasicFunction.sendmsg(msgg='{}'.format(players))
Khailz
  • 89
  • 1
  • 12
  • You don't always seem to be saving references to hold on to the returned values, e.g. `my_reference = getplayer()`, `my_other_reference = hasplayer()`. – TigerhawkT3 Jun 20 '15 at 01:03
  • 1
    `return a` and `return b` ? ..Return what? – Iron Fist Jun 20 '15 at 01:08
  • did you mean `a = hasplayer()` instead of just `hasplayer()`? – rlbond Jun 20 '15 at 01:10
  • Yes I have this in my code now but how am I suppose to get who they are as players when the `getplayer()` is just for getting who sent the message? – Khailz Jun 20 '15 at 01:14
  • agreeing with @KhalilAmmour-خليلعمور, what do you think `a` and `b` are on the lines where you are returning them? – Ryan Haining Jun 20 '15 at 01:22
  • also instead of creating four separate player variables, then creating an empty list, then extending the list by a tuple of players, you could just as well have `players = [getplayer(), getplayer(), getplayer(), getplayer()]` – Ryan Haining Jun 20 '15 at 01:24
  • I thought it would Return it as `a` and `b` if I did that, so leaving it blank will still leave the same thing right? – Khailz Jun 20 '15 at 01:27
  • Using `players = [getplayer(), getplayer(), getplayer(), getplayer()]` doesn't work because it will print the same value each time. That's why I tried assigning each `getplayer()` as a different variable inside so I could use it outside. – Khailz Jun 20 '15 at 01:31
  • Is getplayer() supposed to nested inside players()? – Dan Jun 20 '15 at 02:43
  • This is just the way I happened to do it since it follows the same way I do a similar function in my actual code. Is it bad that I do this? – Khailz Jun 20 '15 at 02:52

0 Answers0