#汇率兑换
循环和if语句的实例:
USA_vs_RMB = 6.7
i = 1
jine_danwei = input('请输入金额(退出请输入Q):')
while jine_danwei != 'Q':
# i = i + 1 计算运算的次数
danweizhi = jine_danwei[-3:]
if danweizhi == 'RMB':
rmb_how_much = jine_danwei[:-3]
rmb = eval(rmb_how_much)
meiyuan = rmb / USA_vs_RMB
print('输出美元金额为:', meiyuan)
elif danweizhi == 'USD':
usd_how_much = jine_danwei[:-3]
usd = eval(usd_how_much)
renminbi = usd * USA_vs_RMB
print('输出的人民币为:', renminbi)
else:
print('没有此项汇率换算,请重新输入')
# 其他情况
print('*********************************************')
jine_danwei = input('请输入金额(退出请输入Q):')
print('已退出!')
===========================================================================
函数调用
def money(JE,EX):
"""
汇率兑换函数
"""
out = JE * EX
return out
def main():
# 汇率换算
USA_vs_RMB = 6.7
#带单位的货币输入
jine_danwei = input('请输入金额:')
huobi_danwei = jine_danwei[-3:]
if huobi_danwei == 'RMB':
exchange_rate = 1 / USA_vs_RMB
elif huobi_danwei == 'USD':
exchange_rate = USA_vs_RMB
else:
exchange_rate = -1
if exchange_rate != -1:
in_money = eval(jine_danwei[:-3])
#调用函数
how_much = money(in_money,exchange_rate)
print('输出金额为:', how_much)
if __name__ == '__main__':
main()
部署完成后进行测试输入金额123RMB,输出的美元金额为18.35820895522388