0

It might be rediculous but I couldn't get my head around the use of global variables in Python. It throws me syntax errors for the use of the keyword

global

even though, I looked it up exactly that way in the documentation.

I know the code is clumsy for its repetative use of global variables. Anyway, how to use them correctly?

import random

r_list = []

my_list =[3,4,45,7,23,33]
match_list=[]
iteration = 0
number_matches = 0

def fill_random_list():
    for i in range(7):
        # print (random.randint(1,49))
        r_list.append(random.randint(1,49))


def return_matches(lista, listb):
    # print set(lista).intersection(listb)
    return set(lista).intersection(listb)


def return_number_matches(l_matches):
    if l_matches:
        return len(l_matches)


def draw_lottery():
    while global number_matches  < 5:'''
                                     File "C:/Lottery.py", line 27
                                     while global number_matches  < 5:
                                                ^
                                     SyntaxError: invalid syntax'''
        global r_list = []
        global match_list = []
        fill_random_list()
        match_list=return_matches(r_list, my_list)
        global number_matches=(return_number_matches(global match_list))
        global iteration+=1
        if number_matches > 4:
            print (str(global iteration) + ';' + str(global number_matches))


def iterate(number):
    for i in enumerate(range(number),1):
        print('Round: ' + str(i[0]))
        draw_lottery()


def main():
    iterate(10)


if __name__ == "__main__":
    main()
royskatt
  • 1,190
  • 2
  • 15
  • 35
  • 3
    Possible duplicate of [Using global variables in a function other than the one that created them](https://stackoverflow.com/questions/423379/using-global-variables-in-a-function-other-than-the-one-that-created-them) – Reza Oct 04 '17 at 11:20
  • How did you find out about the `global` keyword and what did your source say about using it? – quamrana Oct 04 '17 at 11:20

2 Answers2

5

Don't annotate every use of a global variable with global. All you need to do is write global var1, var2 in your function before modifying the variables.

If you're only reading from them you don't need to use global. You only need it if you're assigning to them.

def draw_lottery():
    global number_matches, r_list, match_list, number_matches, iteration

    while number_matches < 5:
        r_list     = []
        match_list = []

        fill_random_list()

        match_list     = return_matches(r_list, my_list)
        number_matches = return_number_matches(match_list)

        iteration += 1

        if number_matches > 4:
            print(str(iteration) + ';' + str(number_matches))

As a matter of good style, avoid using global variables. It's hard to keep track of program state when many functions share the same variables. It's better to use parameters and return values instead, whenever possible.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • > " It's better to use parameters and return values instead" - or use a class if the shared state has to be maintained between functions calls. – bruno desthuilliers Oct 04 '17 at 12:04
1

the problem with your code is quite simple if you want to modify a global variable inside a function you need first to refer to it explicitly, how? be including the line

global variable_name

in your function code, this should occur before reading or modifying this variable

rachid el kedmiri
  • 2,376
  • 2
  • 18
  • 40