UnboundLocalError - Please help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • michaelw209
    New Member
    • Feb 2010
    • 2

    UnboundLocalError - Please help

    How do I fix this? It's just an assignment for school bit it's driving me crazy. low, med, and high are supposed to be initialized at there starting values only once before the loop begins, but it won't work, here's the data.

    Code:
    def main():
        choice = 0
        high = 3000.0
        med = 5000.0
        low = 9000.0
        priority = 0
        size = 0.0
        frequency = 0
    
        
        while choice != 3:
            print '1-add a back up'
            print '2-remove a backup'
            print '3-exit program'
            choice = input  ('make a selection')
            if choice == 1:
                priority = getPriority(priority)
                if priority == 1:
                    addHigh()
                elif priority == 2:
                    addMed()
                elif priority == 3:
                    addLow()
                else:
                    print 'invalid entry'
            
                    
            elif choice == 2:
                priority = getPriority(priority)
                if priority == 1:
                    removeHigh()
                elif priority == 2:
                    removeMed()
                elif priority == 3:
                    removeLow()
                else:
                    print 'invalid entry'
    
            elif choice == 3:
                print 'goodbye'
            else:
                print'invalid entry'
    
    
    def getPriority(priority):
        print '1-high priority'
        print '2-medium priority'
        print '3-low priority'
        priority = input ('enter priority')
        return priority
    
    def getSize():
        size = input  ('enter size in megabytes')
        return size
    
    def getFrequency():
        frequency = input  ('enter frequency')
        return frequency
    
    
        
    def addHigh():
        size = getSize()
        frequency = getFrequency()
        
        high=high - size * frequency
        if high > 0:
            print 'you have' , high , 'MB left'
        else:
            print 'insufficient space'
            high = high + size * frequency
    And here's the traceback:

    Traceback (most recent call last):
    File "F:\programming \Lab Practicum.py", line 131, in <module>
    main()
    File "F:\programming \Lab Practicum.py", line 22, in main
    addHigh()
    File "F:\programming \Lab Practicum.py", line 69, in addHigh
    high=high - size * frequency
    UnboundLocalErr or: local variable 'high' referenced before assignment
    >>>
    PLEASE HELP!!!
    Last edited by bvdet; Feb 26 '10, 03:56 AM. Reason: Add code tags
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Please use code tags when posting code. See Posting Guidelines here.

    It's a matter of scope. You assign a value of 3000.0 to identifier high inside the scope of function main(). Then you attempt to access high inside the function addHigh(). When resolving names, the interpreter first searches the local namespace. If not found, it searches the enclosing scopes from the innermost scope to the outermost. In your case, the next enclosing scope is the global scope. The global namespace for a function is always the module in which the function was defined. If not found in the global namespace, it makes one final check in the built-in namespace. If this fails, a NameError is raised. UnboundLocalErr or is a subclass of NameError and occurs if a variable is referenced before it's defined in a function.

    The interpreter never looks in the namespace of function main() for high, therefore the error.

    You could pass the variable as an argument to function addHigh.
    Code:
    def main():
        #global high
        choice = 0
        high = 3000.0
        med = 5000.0
        low = 9000.0
        priority = 0
        size = 0.0
        frequency = 0
     
     
        while choice != 3:
            print '1-add a back up'
            print '2-remove a backup'
            print '3-exit program'
            choice = input  ('make a selection')
            if choice == 1:
                priority = getPriority(priority)
                if priority == 1:
                    high = addHigh(high)
                    # snip
    
    
    def addHigh(high)
        size = getSize()
        frequency = getFrequency()
     
        high=high - size * frequency
        if high > 0:
            print 'you have' , high , 'MB left'
        else:
            print 'insufficient space'
            high = high + size * frequency
        return high
    You also could define high in the global scope and declare high as a global variable.
    Code:
    high = 3000.0
    med = 5000.0
    low = 9000.0
    
    def main():
        choice = 0
        priority = 0
        size = 0.0
        frequency = 0
        # snip
    
    
    def addHigh():
        global high
        size = getSize()
        frequency = getFrequency()
     
        high=high - size * frequency
        if high > 0:
            print 'you have' , high , 'MB left'
        else:
            print 'insufficient space'
            high = high + size * frequency

    Comment

    • michaelw209
      New Member
      • Feb 2010
      • 2

      #3
      Thank you for your help. I'll try those, hope it works.

      Comment

      Working...