0

So I am new to python and here is my python code:

def main():
    N = 1
    players = 10
    for _ in range(N):
        maxi = 0
        t1 = [3, 6, 7, 5, 3, 5, 6, 2, 9, 1]
        t2 = [2, 7, 0, 9, 3, 6, 0, 6, 2, 6]
        for i in range(players):
            count = t2[i]+1
            flag = 0
            def calcWin(count):
                global maxi,flag
                if(count in t1):
                    flag = 1
                    maxi = maxi + 1
                    t1.remove(count)
                else:
                    count = count + 1
                    if(count<=max(t1)):
                        calcWin(count)

            calcWin(count)
            print(flag)

main()

I want the variable 'maxi' to be accessible inside the function calcWin(). So it declared the variable as global inside the function. But it throws 'NameError'. I want the variable 'maxi' to reinitialize to 0 for each 'N'. Another thing is that the 'flag' variable always prints 0 even though it satisfies the condition 'if(count in t1)'. (Note: I declared 'flag' as global inside the function). Can somebody help me with this?

wohlstad
  • 12,661
  • 10
  • 26
  • 39
Kishan Lal
  • 473
  • 1
  • 5
  • 13
  • The general rule is to avoid global variables as much as possible. The code defines global variables in two nested loops, which brings a chaos – AlexMTX Apr 14 '20 at 09:03
  • @AlexMTX Okay, then how can I achieve this without using global variables? – Kishan Lal Apr 14 '20 at 09:04
  • 1
    @Kishan By passing the required values to the function and getting their updated values by return. – matheburg Apr 14 '20 at 09:09
  • 1
    1/ The `maxi` and `flag` vars defined in your `main` functions are NOT globals - they are local to `main`. 2/ there's way too much code in your function, 3/ you don't need to re-define `calcWin` again and again in each iteration, 4/ there are mainly two ways to avoid globals: a/ passing values to and returning values from your functions and b/ use a class. – bruno desthuilliers Apr 14 '20 at 09:11
  • Btw: If you want to use global variables in Python functions, you may refer to https://stackoverflow.com/questions/423379/using-global-variables-in-a-function – matheburg Apr 14 '20 at 09:21

1 Answers1

0

As you are asking in the comment for a way to do this without global variables, the following could be a proposal. Please understand that I just imitated what I think you intended with your original code. You may have to adapt your algorithm...

def main():
    n = 1
    players = 10
    for _ in range(n):
        maxi_persistent = 0
        t1 = [3, 6, 7, 5, 3, 5, 6, 2, 9, 1]
        t2 = [2, 7, 0, 9, 3, 6, 0, 6, 2, 6]
        for i in range(players):
            def calc_win(count, maxi):
                flag = False
                if count in t1:
                    flag = True
                    maxi += 1
                    t1.remove(count)
                else:
                    count += 1
                    if count <= max(t1):
                        return calc_win(count, maxi)
                return maxi, flag

            maxi_persistent, win_flag = calc_win(count=t2[i] + 1, maxi=maxi_persistent)
            print(win_flag)


if __name__ == "__main__":
    main()

Please note that flag, as you use it in your question, does not need any global handling. It is 0 at each call of the function, so I just set it to 0 (or False) in the beginning of the function instead.

matheburg
  • 2,097
  • 1
  • 19
  • 46