[Tutor] urgent help!!!!!!!!!!!

Alex Hall mehgcap at gmail.com
Fri Nov 18 00:39:41 CET 2011


My mail client stripped new lines, at least on this machine, so I will
just top-post this. Sorry!
Your problem is that you say:
amount=0
def main()...
You then try to use amount in main, but main has no amount in it.
Either move "amount=0" inside main, or put a global reference:
amount=0
def main():
    global amount
    ...

On 11/17/11, ADRIAN KELLY <kellyadrian at hotmail.com> wrote:
>
> i know i'm stupid but i have tried everything to get one line of text
> working, i have written out pseudo and read every website.....now i am
> getting this error............
> Traceback (most recent call last):  File "F:\VTOS
> ATHLONE\PYTHON_VTOS\foreign exchange\f_ex4 - Copy.py", line 24, in <module>
>   main()  File "F:\VTOS ATHLONE\PYTHON_VTOS\foreign exchange\f_ex4 -
> Copy.py", line 14, in main    while amount<50:UnboundLocalError: local
> variable 'amount' referenced before assignment>>>
>
>
> def exchange(cash_in):    euro=1    dollar=1.35    base=50    if
> cash_in>base:        totalreturn=cash_in*dollar    else:
> totalreturn=0    return totalreturn
> amount=0def main():    while amount<50:        amount = raw_input(float('how
> much do you want to change:'))    if amount<50:        total=0        print
> 'enter an amount over 50: '    else:        total=exchange(amount)
> print 'Your exchange comes to: ',total
>
>
>
>
> Adrian Kelly
> 1 Bramble Close
>
> Baylough
>
> Athlone
>
> County Westmeath
>
> 0879495663
>
>
> From: waynejwerner at gmail.com
> Date: Thu, 17 Nov 2011 16:53:59 -0600
> Subject: Re: [Tutor] please help - stuck for hours
> To: kellyadrian at hotmail.com
> CC: tutor at python.org
>
> On Thu, Nov 17, 2011 at 4:32 PM, ADRIAN KELLY <kellyadrian at hotmail.com>
> wrote:
>
>
>
>
>
>
>
>
> thanks very much, great response really really appreciated it and now i
> understand. i hate to ask again but can you see why it won't print the
> 'enter and amount over 50' in the right place??
>
>
> Computers are unfailingly stupid machines. They will do whatever wrong thing
> you tell them to do every single time.
>
>  <snip>def main():    amount=0    while amount<50:        amount =
> input('how much do you want to change:')
>
>         print 'enter an amount over €50: '    else:
> total=exchange(amount)        print 'Your exchange comes to: ',total
>
>
> Sometimes it helps writing out the logic in steps before you translate it to
> code. In this case my guess is that these are the steps you want:
>   1. Get a value from the user (you're still using input - stop that, it's
> dangerous! input is   only a good function in 3.x where it replaces
> raw_input)
>
>    2. If the value is less than 50, tell the user to enter an amount > 50
> and repeat step 1
>   3. Otherwise, exchange the amount and display that.
> Right now, these are the steps that you're doing:
>
>
>   1. Get a value from the user
>   2. Display the error message
>   3. If the value is < 50, go to 1
>   4. exchange the amount
>
>
>   5. display the amount.
> HTH,Wayne 		 	   		


-- 
Have a great day,
Alex (msg sent from GMail website)
mehgcap at gmail.com; http://www.facebook.com/mehgcap


More information about the Tutor mailing list