473,320 Members | 1,722 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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.

Expand|Select|Wrap|Line Numbers
  1. def main():
  2.     choice = 0
  3.     high = 3000.0
  4.     med = 5000.0
  5.     low = 9000.0
  6.     priority = 0
  7.     size = 0.0
  8.     frequency = 0
  9.  
  10.  
  11.     while choice != 3:
  12.         print '1-add a back up'
  13.         print '2-remove a backup'
  14.         print '3-exit program'
  15.         choice = input  ('make a selection')
  16.         if choice == 1:
  17.             priority = getPriority(priority)
  18.             if priority == 1:
  19.                 addHigh()
  20.             elif priority == 2:
  21.                 addMed()
  22.             elif priority == 3:
  23.                 addLow()
  24.             else:
  25.                 print 'invalid entry'
  26.  
  27.  
  28.         elif choice == 2:
  29.             priority = getPriority(priority)
  30.             if priority == 1:
  31.                 removeHigh()
  32.             elif priority == 2:
  33.                 removeMed()
  34.             elif priority == 3:
  35.                 removeLow()
  36.             else:
  37.                 print 'invalid entry'
  38.  
  39.         elif choice == 3:
  40.             print 'goodbye'
  41.         else:
  42.             print'invalid entry'
  43.  
  44.  
  45. def getPriority(priority):
  46.     print '1-high priority'
  47.     print '2-medium priority'
  48.     print '3-low priority'
  49.     priority = input ('enter priority')
  50.     return priority
  51.  
  52. def getSize():
  53.     size = input  ('enter size in megabytes')
  54.     return size
  55.  
  56. def getFrequency():
  57.     frequency = input  ('enter frequency')
  58.     return frequency
  59.  
  60.  
  61.  
  62. def addHigh():
  63.     size = getSize()
  64.     frequency = getFrequency()
  65.  
  66.     high=high - size * frequency
  67.     if high > 0:
  68.         print 'you have' , high , 'MB left'
  69.     else:
  70.         print 'insufficient space'
  71.         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
UnboundLocalError: local variable 'high' referenced before assignment
>>>
PLEASE HELP!!!
Feb 26 '10 #1

✓ answered by bvdet

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. UnboundLocalError 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.
Expand|Select|Wrap|Line Numbers
  1. def main():
  2.     #global high
  3.     choice = 0
  4.     high = 3000.0
  5.     med = 5000.0
  6.     low = 9000.0
  7.     priority = 0
  8.     size = 0.0
  9.     frequency = 0
  10.  
  11.  
  12.     while choice != 3:
  13.         print '1-add a back up'
  14.         print '2-remove a backup'
  15.         print '3-exit program'
  16.         choice = input  ('make a selection')
  17.         if choice == 1:
  18.             priority = getPriority(priority)
  19.             if priority == 1:
  20.                 high = addHigh(high)
  21.                 # snip
  22.  
  23.  
  24. def addHigh(high)
  25.     size = getSize()
  26.     frequency = getFrequency()
  27.  
  28.     high=high - size * frequency
  29.     if high > 0:
  30.         print 'you have' , high , 'MB left'
  31.     else:
  32.         print 'insufficient space'
  33.         high = high + size * frequency
  34.     return high
You also could define high in the global scope and declare high as a global variable.
Expand|Select|Wrap|Line Numbers
  1. high = 3000.0
  2. med = 5000.0
  3. low = 9000.0
  4.  
  5. def main():
  6.     choice = 0
  7.     priority = 0
  8.     size = 0.0
  9.     frequency = 0
  10.     # snip
  11.  
  12.  
  13. def addHigh():
  14.     global high
  15.     size = getSize()
  16.     frequency = getFrequency()
  17.  
  18.     high=high - size * frequency
  19.     if high > 0:
  20.         print 'you have' , high , 'MB left'
  21.     else:
  22.         print 'insufficient space'
  23.         high = high + size * frequency

2 1667
bvdet
2,851 Expert Mod 2GB
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. UnboundLocalError 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.
Expand|Select|Wrap|Line Numbers
  1. def main():
  2.     #global high
  3.     choice = 0
  4.     high = 3000.0
  5.     med = 5000.0
  6.     low = 9000.0
  7.     priority = 0
  8.     size = 0.0
  9.     frequency = 0
  10.  
  11.  
  12.     while choice != 3:
  13.         print '1-add a back up'
  14.         print '2-remove a backup'
  15.         print '3-exit program'
  16.         choice = input  ('make a selection')
  17.         if choice == 1:
  18.             priority = getPriority(priority)
  19.             if priority == 1:
  20.                 high = addHigh(high)
  21.                 # snip
  22.  
  23.  
  24. def addHigh(high)
  25.     size = getSize()
  26.     frequency = getFrequency()
  27.  
  28.     high=high - size * frequency
  29.     if high > 0:
  30.         print 'you have' , high , 'MB left'
  31.     else:
  32.         print 'insufficient space'
  33.         high = high + size * frequency
  34.     return high
You also could define high in the global scope and declare high as a global variable.
Expand|Select|Wrap|Line Numbers
  1. high = 3000.0
  2. med = 5000.0
  3. low = 9000.0
  4.  
  5. def main():
  6.     choice = 0
  7.     priority = 0
  8.     size = 0.0
  9.     frequency = 0
  10.     # snip
  11.  
  12.  
  13. def addHigh():
  14.     global high
  15.     size = getSize()
  16.     frequency = getFrequency()
  17.  
  18.     high=high - size * frequency
  19.     if high > 0:
  20.         print 'you have' , high , 'MB left'
  21.     else:
  22.         print 'insufficient space'
  23.         high = high + size * frequency
Feb 26 '10 #2
Thank you for your help. I'll try those, hope it works.
Feb 26 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Brad Clements | last post by:
I was going to file this as a bug in the tracker, but maybe it's not really a bug. Poor Python code does what I consider to be unexpected. What's your opinion? With Python 2.3.2 (but also...
6
by: Alex Gittens | last post by:
I'm trying to define a function that prints fields of given widths with specified alignments; to do so, I wrote some helper functions nested inside of the print function itself. I'm getting an...
2
by: silverburgh.meryl | last post by:
Can you please tell me what is the meaning this error in general? UnboundLocalError: local variable 'colorIndex' referenced before assignment In my python script, I have a variable define and...
8
by: David Bear | last post by:
I'm attempting to use the cgi module with code like this: import cgi fo = cgi.FieldStorage() # form field names are in the form if 'name:part' keys = fo.keys() for i in keys: try:...
15
by: Paddy | last post by:
Hi, I am trying to work out why I get UnboundLocalError when accessing an int from a function where the int is at the global scope, without explicitly declaring it as global but not when accessing...
9
by: Camellia | last post by:
hi all why it generates an "UnboundLocalError" when I do the following: <code> .... def main(): number = number() number_user = user_guess() while number_user != number:
2
by: Konstantinos Pachopoulos | last post by:
Hi, i have a problem, the source of which is probably the fact, that i have not understood how to declare global variables - I use the Jython compiler, but i think this is a Python issue... ...
8
by: defn noob | last post by:
isPrime works when just calling a nbr but not when iterating on a list, why? adding x=1 makes it work though but why do I have to add it? Is there a cleaner way to do it? def isPrime(nbr):...
1
by: nongnaeja | last post by:
I don't know ,why it's error This my code from PIL import Image def ch_red(tmp): R = tmp G = tmp B = tmp
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.