I am working on a currency converter for my computing GCSE at school and I have no idea what I am doing wrong here.
My code is:
PD = 1.66401 # declare pounds to dollars exchange rate
def Convert(ex): #converting sub... ex = exchange rate
amount = input("Enter amount to convert: ") #get amount to 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 to enter new exchange rate
if ex == PD: #check what exchange rate to change (needed because final version will 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 to dollars
elif menu == "2":
Change(PD) #Call sub...change pounds to dollars exchange rate
When I convert it works correctly but when changing exchange rate i get the 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 to dollars echange rate
File "F:/USB/Computing/Programming/Python/test2.py", line 10, in Change
if ex == PD: #check what exchange rate to change (needed because final version will have alot more options
UnboundLocalError: local variable 'PD' referenced before assignment
I have searched around but no answer has been very helpful if you could try and keep any explanations as simple as possible and provide an example of where Im going wrong and what i should be doing that would be greatly appreciated.