[Tutor] local var error
alan.gauld at btinternet.com
Mon Mar 15 20:17:18 CET 2010
"Prasad" <microteacher at gmail.com> wrote
>I am writing a function to find correct transformer lamination for
> specific wattage. It gives an error message:
> UnboundLocalError: local variable 'stdArea' referenced before assignment
Please al;ways senmd the full error text not just the last line.
However in your case we can do without....
> voltAmp=raw_input("How much power in Watts you want to handle?:)
voltAmp is a string
You probabluy want to convert it to an int or float
voltAmp=int( raw_input("How much power in Watts you want to
handle?: ) )
> print vamaxList
> for x in vamaxList:
You could replace all of that with
for x in sorted(dictLam):
> if x<voltAmp:
This is comparing a number to a string.
> pass#print ".",
This only gets exercised if the test is false, which, if you are comparing
to a string might never be true. So it might not get excercised vand the
variable might never get created. Better to initialise it to a default
(zero?) outside the loop if it is only used inside a conditional. Or catch
the exception if no sensible default exists.
> I am creating a variable in the for loop. Shouldn't it work outside it?
It will if you create it, I suspect you never do.
Author of the Learn to Program web site
More information about the Tutor