[Tutor] local var error
Alan Gauld
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?: ) )
> dictLam={0.2:32,2:17,5:12,6:1,7:74,10:2,18:45,31:15,45:33,75:13,156:5,494:43}
> temp=dictLam.keys()
> temp.sort()
> vamaxList=temp
> 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 ".",
> else:
> stdArea=(math.sqrt(x))/6
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
value
(zero?) outside the loop if it is only used inside a conditional. Or catch
the exception if no sensible default exists.
> windArea=(math.sqrt(stdArea))*0.75
> 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.
HTH,
--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/
More information about the Tutor
mailing list