0

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.

Kevin
  • 74,910
  • 12
  • 133
  • 166
Sabersimon
  • 50
  • 2
  • 8
  • Global *reading* works the way you intend. Global *writing* requires the `global` keyword. This is answered here: http://stackoverflow.com/a/423596/145400 – Dr. Jan-Philip Gehrcke Jan 22 '15 at 20:05
  • possible duplicate of [Using global variables in a function other than the one that created them](http://stackoverflow.com/questions/423379/using-global-variables-in-a-function-other-than-the-one-that-created-them) – Dr. Jan-Philip Gehrcke Jan 22 '15 at 20:05
  • I don't think you can add items to a dict using `+`. Are you trying to do `d[num2] = primes`? – Kevin Jan 22 '15 at 20:09

2 Answers2

0

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
    ...
tdelaney
  • 73,364
  • 6
  • 83
  • 116
  • Good start, but next OP is going to get `TypeError: unsupported operand type(s) for +=: 'dict' and 'dict'`. – Kevin Jan 22 '15 at 20:13
  • this does not work due to the fact that I want d to persist even though I have returned. It does not work. – Sabersimon Jan 22 '15 at 20:24
  • @Kevin - yes, but that's the next question. `d.update({num2:primes})` or more sanely `d[num2] = primes` would do it... and OP wouldn't need a global for that. But that doesn't explain the original error. – tdelaney Jan 22 '15 at 20:45
  • @Sabersimon - when you say `global d` you are telling python to use the perisistent `d` and not create a temporary one in the function local namespace. – tdelaney Jan 22 '15 at 20:46
0

Global variables must use the 'global' keyword when their value is being changed.

YourDict = {"Key1", "Value1"}

def Change():
    global YourDict
    YourDict.keys["Key1"] = "Value2"
devon t
  • 55
  • 2
  • 8