In: Computer Science
Hello, trying to create a three way currency conversion on python. One of the things I want to do is create an error message whenever there is an incorrect currency entered. So far, I have managed to display the error message in these two cases: when both From and To currencies are incorrect, and when only the From currency is incorrect. What can I do so that my program can also display a message when the To currency is only incorrect.
money = float(input("Please enter the From amount to be
converted: "))
fromCurrency = input("Please enter the From currency (USD, EUR, or
JYP): ")
toCurrency = input("Please enter the To currency (USD, EUR, or
JYP): ")
## conversions taken as of 9/26/19
# 1 USD = .91 EUR (1.1 factor), 1 USD = 107.63 JYP (.00929 factor),
1 EUR = 117.81 JYP (.0084 factor)
usdEur = money / 1.1
eurUsd = money * 1.1
usdJyp = money / .00929
jypUsd = money * .00929
eurJyp = money / .00848
jypEur = money * .00848
# Conversion from USD to other currencies
if fromCurrency.lower() == ("usd"):
if toCurrency.lower() == ("eur"):
result = usdEur
if toCurrency.lower() == ("jyp"):
result = usdJyp
print(money, fromCurrency, "equals", result, toCurrency)
elif fromCurrency.lower() == ("eur"):
if toCurrency.lower() == ("usd"):
result = eurUsd
if toCurrency.lower() == ("jyp"):
result = eurJyp
print(money, fromCurrency, "equals", result, toCurrency)
elif fromCurrency.lower() == ("jyp"):
if toCurrency.lower() == ("usd"):
result = jypUsd
if toCurrency.lower() == ("eur"):
result = jypEur
print(money, fromCurrency, "equals", result, toCurrency)
else: #does not work when 'to' currency is wrong
print("Error! Currency not available for conversion")
Thanks for the question, Below is the a much cleaner code. Instead of doing lower() every time, do it when you read the currency from the user to upper. There can be 6 conversions and 1 valid conversion when the from and to currency are same so total 7 valid conversions, anything else is an invalid conversion and the last else statement takes care of that. Here is the updated code. Hope it solves your problem. thank Do comment incase you have any doubts, will repsond. ============================================================================== money = float(input("Please enter the From amount to be converted: ")) fromCurrency = input("Please enter the From currency (USD, EUR, or JYP): ").upper() toCurrency = input("Please enter the To currency (USD, EUR, or JYP): ").upper() ## conversions taken as of 9/26/19 # 1 USD = .91 EUR (1.1 factor), 1 USD = 107.63 JYP (.00929 factor), 1 EUR = 117.81 JYP (.0084 factor) usdEur = money / 1.1 eurUsd = money * 1.1 usdJyp = money / .00929 jypUsd = money * .00929 eurJyp = money / .00848 jypEur = money * .00848 if fromCurrency == toCurrency and (fromCurrency == 'USD' or fromCurrency == 'EUR' or fromCurrency == 'JYP'): print(money, fromCurrency, "equals", money, toCurrency) elif fromCurrency == 'USD' and toCurrency == 'EUR': print(money, fromCurrency, "equals", usdEur, toCurrency) elif fromCurrency == 'USD' and toCurrency == 'JYP': print(money, fromCurrency, "equals", usdJyp, toCurrency) elif fromCurrency == 'EUR' and toCurrency == 'USD': print(money, fromCurrency, "equals", eurUsd, toCurrency) elif fromCurrency == 'EUR' and toCurrency == 'JYP': print(money, fromCurrency, "equals", eurJyp, toCurrency) elif fromCurrency == 'JYP' and toCurrency == 'USD': print(money, fromCurrency, "equals", jypUsd, toCurrency) elif fromCurrency == 'JYP' and toCurrency == 'EUR': print(money, fromCurrency, "equals", jypEur, toCurrency) # if the above currency dont match then the from and to currency are incorrect else: print("Error! Currency not available for conversion")