Reputation: 95
I create two lists, a, b with 10 random numbers from 0 to 61 and then I compare the lists if they have common numbers or not.
I store the common numbers in a separate list.
If the list does have numbers in it the commonCount is going up and if the list is empty the noCommonCount is going up.
But when I want to print the counts after I rand the function 10 times it prints out 0.
I don't know why because I declared the variables commonCount and noCommonCount outside the function.
import random
noCommonCount = 0
commonCount = 0
def list_overlap():
a = []
b = []
count = 0
while count < 10:
count = count + 1
a.append(random.randint(0, 61))
b.append(random.randint(0, 61))
commonNumbers = []
for i in a:
if i in b:
if i not in commonNumbers:
commonNumbers.append(i)
if not commonNumbers:
noCommonCount + 1
else:
commonCount + 1
functionCount = 0
while functionCount < 10:
functionCount = functionCount + 1
list_overlap()
print(noCommonCount)
print(commonCount)
Upvotes: 0
Views: 195
Reputation: 5333
For a function modifying a variable declared on outer scope additionally a declaration of the form
global variable_name
is required in the function (typically directly after function declaration.
Upvotes: 2