求python代码。货币转换人民币对美元。格式为: 人民币:20 美元:xxx

求python代码。货币转换人民币对美元。格式为: 人民币:20 美元:xxx,第1张

 

#获取实时铅燃启汇率,根据汇率换段悄算

import requests

import re

import json

 url='https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?     query=1%E7%BE%8E%E5%85%83%E7%AD%89%E4%BA%8E%E5%A4%9A%E5%B0%91%E4%BA%BA%E6%B0%91%E5%B8%81&co=&resource_id=6017&t=1560336236210&cardId=6017&ie=utf8&槐如oe=gbk&cb=op_aladdin_callback&format=json&tn=baidu&cb=jQuery110201293669134906772_1560336178301&_=1560336178313'

res=requests.get(url)

res.coding='gbk'

changge=res.text

changge=re.findall('\{.*\}',changge)

changge=json.loads(changge[0])

changge=changge['data'][0]['content2']

n=re.findall('1人民币元=(.*)美元',changge)

n=float(n[0])

usd=rnb/n

print('人民币:{0} 美元:{1}'.format(rnb,round(usd,2)))

import re

money = input().lower()

tmp=re.findall('usd|rmb',money)

if len(tmp) 好毕祥!= 1:

    print('wrong')

money=re.sub(tmp[0],'',money)

try:

    num=float(money)

    if 'usd' in tmp:

        print('RMB%.2f'%(num*6.78))

    else:

   友搏     print('USD%.2f'数毕%(num/6.78))

except:

    print('wrong')

# 设置人民币美元汇率

exchange_rate = 6.74

# 输入函数

money_str = input("您好,请您输入含有rmb(人民币),usa(美元)的字母的金额:")

print("您输入的金额谨悔岁为:", money_str)

# 截取的输入单位

unit = money_str[-3:]

print("您截取的单位为:", unit)

if unit == "rmb":

# 截取输入函数的数值

rmb_str_value = money_str[:-3]

# 字符型数据转化为浮点型数据

rmb_float = float(rmb_str_value)

# 计算两者的汇率

usa = rmb_float / exchange_rate

# 输出函数

print("经过换算后的美元金额为:", usa)

elif unit == "usa":

# 截取输入函数的数值

usa_str_value = money_str[:-3]

# 字符型数据转化为浮点型数据

usa_float = float(usa_str_value)

# 计算两者的汇率

rmb = usa_float * exchange_rate

# 输出函数

print("经过换前李算后的人民币金祥睁额为:", rmb)

else:

print("您输入的格式有错误,请关闭之后重新输入!


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/12225094.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-22
下一篇2023-05-22

发表评论

登录后才能评论

评论列表(0条)

    保存