[Tutor] local variable referenced before assignment

ADRIAN KELLY kellyadrian at hotmail.com
Thu Nov 17 20:19:06 CET 2011


you are spot on......... thanks very much i understand the problem now and its been solved.
very clear help
thanks, adrian

 

  



> Date: Thu, 17 Nov 2011 14:01:19 -0500
> From: d at davea.name
> To: kellyadrian at hotmail.com
> CC: tutor at python.org
> Subject: Re: [Tutor] local variable referenced before assignment
> 
> 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
> 
 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20111117/83ffb31f/attachment.html>


More information about the Tutor mailing list