Python referenced local variable before assignment -


i working on currency converter computing gcse @ school , have no idea doing wrong here.

my code is:

pd = 1.66401 # declare pounds dollars exchange rate  def convert(ex): #converting sub... ex = exchange rate     amount = input("enter amount convert: ") #get amount convert     result = round(float(amount) * float(ex),2) #calculate result using pd rate     print("result = " + str(result)) #print result  def change(ex): #change sub... ex = echange rate     newex = input("enter new exchange rate: ") #allow user enter new exchange rate     if ex == pd: #check exchange rate change (needed because final version have alot more options         pd = float(newex) #change exchange rate  #display menu menu = input("1.convert\n2.modify exchange rate\nplease select option: ") if menu == "1":     convert(pd) #call sub...convert using pounds dollars elif menu == "2":     change(pd) #call sub...change pounds dollars exchange rate 

when convert works correctly when changing exchange rate following error:

traceback (most recent call last):   file "f:/usb/computing/programming/python/test2.py", line 18, in <module>     change(pd) #call sub...change pounds dollars echange rate   file "f:/usb/computing/programming/python/test2.py", line 10, in change     if ex == pd: #check exchange rate change (needed because final version have alot more options unboundlocalerror: local variable 'pd' referenced before assignment 

i have searched around no answer has been helpful if try , keep explanations simple possible , provide example of im going wrong , should doing appreciated.

you can access global variables fine inside functions. when want modify values however, need first use global , declare them being in global scope:

def change(ex):     ##########     global pd     ##########     newex = input("enter new exchange rate: ")     if ex == pd:         pd = float(newex) 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -