从 USD 转换
Coddy 货币转换器:Python 项目 课程的第 4 课,共 5 课。
现在来创建另一个辅助函数:from_usd。
挑战
中等再创建一个名为 from_usd 的函数,用于获取要转换成的货币和USD 金额。
该函数会返回输入货币对应的值,将结果四舍五入到小数点后 4 位。
处理与 to_usd 相同的错误。
不要删除
to_usd函数,后面还会用到它!
亲手试试
CURRENCIES = {
'USD': 1,
'EUR': 1.06,
'YEN': 0.0067,
'GBP': 1.23,
'AUD': 0.64,
'CAD': 0.74
}
def to_usd(from_currency, amount):
if from_currency not in CURRENCIES:
raise Exception(f'{from_currency} is not supported')
elif amount < 0:
raise Exception(f'Invalid amount')
else:
res = CURRENCIES[from_currency] * amount
return res货币转换器:Python 项目中的所有课
自己练习: Python 在线编译器