Hi, trying to make a converter but is running into an issue of displaying an error message (at the bottom of the program) when currencies are entered incorrectly. I was suggested after adding result = 0, I should test for result == 0. With only making necessary changes can you explain what this means to my program and how?
result = 0
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")
Changes made to the program:
Program:
Note: Program is explained using comments in the code.
Code:
#initialise result to 0
result = 0
#input money from user
money = float(input("Please enter the From amount to be converted:
"))
#input from currency
fromCurrency = input("Please enter the From currency (USD, EUR, or
JYP): ")
#input to currnecy
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)
#If conversion is from USD to EUR
usdEur = money / 1.1
#If conversion is from EUR to USD
eurUsd = money * 1.1
#If conversion is from USD to JYP
usdJyp = money / .00929
#If conversion is from JYP to USD
jypUsd = money * .00929
#If conversion is from EUR to JYP
eurJyp = money / .00848
#If conversion is from JYP to EUR
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
# Conversion from EUR to other currencies
elif fromCurrency.lower() == ("eur"):
if toCurrency.lower() == ("usd"):
result = eurUsd
if toCurrency.lower() == ("jyp"):
result = eurJyp
# Conversion from JYP to other currencies
elif fromCurrency.lower() == ("jyp"):
if toCurrency.lower() == ("usd"):
result = jypUsd
if toCurrency.lower() == ("eur"):
result = jypEur
#displays error message if any of fromCurrency or toCurrency is invalid
#If any of the fromCurrency or toCurrency is invalid(i.e not USD
or EUR or JYP)
#Then the if conditions above wont satisfy and the result will be
same as initialised
# that is 0.
# If money entered is 0 then though fromCurrency and toCurrency
entered are
# valid result will be 0.
#So the condition to check for displaying error meassage is
result==0 AND money!=0
if(result==0 and money!=0):
print("Error! Currency not available for
conversion")
elif(money==0):
print("Money entered is 0")
else:
print(money, fromCurrency, "equals", result,
toCurrency)
Output:
Get Answers For Free
Most questions answered within 1 hours.