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. -
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
UnboundLocalError: local variable 'high' referenced before assignment
>>>
PLEASE HELP!!!
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. - 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. - 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
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. - 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. - 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
Thank you for your help. I'll try those, hope it works.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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:...
|
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...
|
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:
|
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...
...
|
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):...
|
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
|
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...
|
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...
|
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...
|
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...
|
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)...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
|
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
| |