1

Only 5 currencies are to be used in a python currency converter tool––(USD, EUR, CAD, GBP, and CHF). If user inputs a different currency in convertFrom or convertTo, how do I add an error message informing user to try again?

Conversion

convertFrom = input("What currency would you like to convert from? ")

amount = int(input("How much of that currency would you like to convert? "))

convertTo = input("Which currency would you like to convert to? ")

2 Answers 2

2

You could use a list of valid currencies

validCurrencies = ["EUR","GBP","USD","CAD", "CHF"]

Then you can validate the input by

currencyFrom = ""

while not currencyFrom in validCurrencies:
  currencyFrom = input("What currency would you like to convert from: (e.g. GBP)").upper()
  if not currencyFrom in validCurrencies:
    print("Invalid Currency, Please Try again")
print("correct Currency please follow next instructions")

output:

What currency would you like to convert from: (e.g. GBP) uuu
Invalid Currency, Please Try again
What currency would you like to convert from: (e.g. GBP) Gbp
correct Currency please follow next instructions

You can do the same for convertTo and amount

Sign up to request clarification or add additional context in comments.

Comments

1

Create a list of currencies that your program supports.

When the user enters a currency, before converting it, check if that currency exists in your list. If it does not, then show an error message. Otherwise, go ahead with the conversion

supported_currencies = ['USD', ...]
convertFrom = input("What currency would you like to convert from? ")
if convertFrom in supported_currencies:
    convert()
else:
     print("currency not supported")

You can also check for convertTo variable whether it exists in the supported list of currencies or not.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.