不知道下面代码能否满足你的要求
"""
022年11月20日人民币与美元的汇率为:7.1199人民币≈1美元。
(1)编写cny2usd函数,该函数接收一个参数,形式类似“20cny”,函数输出为人民币对应的美元数量(保留2位小数),如果格式不对,提示用户输入错误;
(2)编写usd2cny函数,该函数接收一个参数,形式类似“20usd”,函数输出为美元对应的人民币数量(保留2位小数),如果格式不对,提示用户输入错误;
(3)编写exchange函数,该函数接收一个或多个形如“20cny”、“20usd”参数,在该函数内调用前面两个函数,输出计算后的结果。
"""
import re
change = 7.1199
def cny2usd(money):
if money == '0':
print("没有输入转换币种")
elif not money.lower().endswith('cny'):
print('错误币种')
else:
res = re.findall('^(\d+)cny', money.lower())
if not res:
print("发生异常:输入错误")
return
usd = float(money[:-3]) / change
print('人民币转换为美元:%.2f' % usd)
def usd2cny(money):
if money == '0':
print("没有输入转换币种")
elif not money.lower().endswith('usd'):
print('错误币种')
else:
res = re.findall('^(\d+)usd', money.lower())
if not res:
print("发生异常:输入错误")
return
usd = float(money[:-3]) * change
print('美元转换为人民币:%.2f' % usd)
def exchange(*para):
for each in para:
if each.lower().endswith('usd'):
usd2cny(each)
else:
cny2usd(each)
cny2usd('0')
cny2usd('2a0cny')
cny2usd('20cny')
cny2usd('20CNY')
cny2usd('20Cny')
cny2usd('2n')
usd2cny('20usd')
exchange('20cny','20usd')
exchange('20ny','20USD','30usd','20en')