[Tutor] local variable referenced before assignment
Dave Angel
d at davea.name
Thu Nov 17 20:01:19 CET 2011
On 11/17/2011 01:47 PM, ADRIAN KELLY wrote:
> hi all,keep getting the above error, can't understand or fix it, can anyone help.
> def exchange(): euro=1 dollar=1.35 base=50 amount = input ('how much do you want to change') if amount>base: totalreturn=amount*dollar else: print 'not enough' return totalreturn
> print exchange()
>
>
You've been doing better, but this one has lost its formatting
entirely. Are you posting in text mode?
That's not the entire error message. If you examined the entire
traceback, it'd identify the line with the problem, and the particular
variable that's being used before it's been defined.
But since the function is small, i can guess my way through. The
variable is apparently "totalreturn". And when you go through the else
clause you completely miss the assignment to it. When you have a
mechanism like:
if x > y:
newval = 49
else:
print "error message"
#newval = -5
return newval
Without that second assignment, you'll get "the above error" whenever
the else condition prevails. newval has no value, and in fact doesn't
exist if you go through the else clause.
--
DaveA
More information about the Tutor
mailing list