Search code examples
pythondictionaryglobal-variablesprimes

Trying to use a global dict in python3


I am trying to make a global dictionary in python3 to store an ongoing set of key value pairs. For some reason I says:

1 10
[1, 10]
1 10
Traceback (most recent call last):
  File "prime1.py", line 51, in <module>
    m = [dict_check(i) for i in inPut]
  File "prime1.py", line 51, in <listcomp>
    m = [dict_check(i) for i in inPut]
  File "prime1.py", line 17, in dict_check
    if not num2 in d:
UnboundLocalError: local variable 'd' referenced before assignment

Here is my code:

#!/usr/bin/env python3

from sys        import stdin
from math       import sqrt

d = {}
def dict_check(num) :
    print (num)
    if len(num) != 2:
        num1 = num2 = num[0]
    else :
        num1,num2 = num 
        print(num1,num2)
    primes = []
    if num1 > num2 :
        num1, num2 = num2, num1 
    if not num2 in d:
        while num2 >= 1 :
            if num2 in d:
                primes += d[num2]
                break       
            else :
                if is_prime(num2) :
                    primes += [num2]
                num2 -= 1
        d += {num2:primes}  
    newList = []
    print(d)
    for l in d[num2] :
        if l < num1 :
            break
        newList += [l]  
    return sorted(newList)     



def is_prime (n) :
    assert n > 0
    if n == 1 :
        return False
    if n == 2 :
        return True
    if (n % 2) == 0 :
        return False
    for i in range(3, int(sqrt(n)) + 1, 2) :
        if (n % i) == 0 :
            return False
    return True 

if __name__ == "__main__" :

    inPut = ([int(v) for v in s.split()] for s in stdin)
    m = [dict_check(i) for i in inPut]
    for v in zip(*m) :
        v = v
        print(v[0])

I don't understand why this is giving me the error. I have declared it outside of the def so it should be global. I have done this before and it does not seem to be working.


Solution

  • d += {num2:primes} is ambiguous to python. d is a local on the left hand side and a global on the right hand side. You can fix the problem by putting global d at the top of the function.

    def dict_check(num) :
        global d
        ...