This is a great example of where a class would be very helpful. By declaring PD as a class property, you can use the @property decorator to keep your code organized and not have to worry about global variables.
class CurrencyConverter(object):
def __init__(self):
"""
Set the default exchange rate to Pounds to U.S. Dollars
"""
self.PD = 1.66401
@property
def PD(self):
return self._PD
@PD.setter
def PD(self, value):
"""
Set and ensure PD is a float value.
"""
self._PD = float(value)
def convert(self):
"""
Takes user input and returns exchanged value.
"""
amount = input("Enter amount to convert: ")
result = round(float(amount) * self.PD, 2)
print "result = %.2f\n" % result
def change(self):
"""
Takes user input and changes exchange rate.
"""
newex = input("New exchange rate: ")
self.PD = newex
The bonus with this method is that if your next assignment is to extend your currency converter, it'll be easy to add methods or inherit this class like so:
class CanadianExchangeRate(CurrencyConverter):
"""
Converts Canadian Dollars to U.S. Dollars.
"""
def __init__(self):
self.PD = 0.90