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()
asked Oct 4, 2017 at 11:18
2

2 Answers 2

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.

answered Oct 4, 2017 at 11:26
Sign up to request clarification or add additional context in comments.

1 Comment

> " 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.
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

answered Oct 4, 2017 at 11:24

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.