仅以Python货币currencies
使用5个converter tool––(USD, EUR, CAD, GBP, and CHF)
。如果用户在convertFrom or convertTo
中输入其他货币,如何添加错误消息通知用户try again
?
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? ")
答案 0 :(得分:1)
您可以使用有效货币列表
validCurrencies = ["EUR","GBP","USD","CAD", "CHF"]
然后您可以通过
验证输入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")
输出:
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
您可以对convertTo和amount进行相同操作
答案 1 :(得分:0)
创建程序支持的货币列表。
当用户输入货币时,在进行转换之前,请检查列表中是否存在该货币。如果不是,则显示错误消息。否则,请继续进行转换
supported_currencies = ['USD', ...]
convertFrom = input("What currency would you like to convert from? ")
convert()
else:
print("currency not supported")
您还可以检查convertTo变量是否在受支持的货币列表中。