I would use a dictionary like so*:
if currency1 == 'GBP':
number = {'USD':1.64, 'EUR':1.20552, 'JPY':171.181}.get(currency2, 1)
Also, notice that I used dict.get
here. If currency2
is not found in the dictionary, number
will be assigned to 1
and a KeyError
will not be raised. However, you can choose anything you want for the default value (or omit it entirely and have None
be the default).
Finally, you should note that putting float literals inside the float
built-in is unnecessary.
*Note: If you plan to use the dictionary in multiple places, don't keep recreating it. Instead, save it in a variable like so:
my_dict = {'USD':1.64, 'EUR':1.20552, 'JPY':171.181}
if currency1 == 'GBP':
number = my_dict.get(currency2, 1)