案例:python实现单位转换

1.根据输入内容判断单位类型
2.完成温度互转,华氏温度与摄氏温度,
3.完成长度互转,中国与美国长度单位
4.完成货币互转,美元与人民币(汇率以你作业当天为准)

content = {'T':'温度转换',
           'L':'长度转换',
           'D':'货币转换'
           }
for k,v in content.items():
    print(k,v)
switch_type = input('请输入转换类型:')
if switch_type == 'T':
    temp = input('请输入温度(示例:1C或者1F):')
    if temp.endswith('C'):
        temp = float(temp.strip('C'))
        Tf = (9/5)*temp + 32
        print(f'F=9/5*{temp}+32={Tf}F')
    elif temp.endswith('F'):
        temp = float(temp.strip('F'))
        Tc = (5/9)*(temp - 32)
        print(f'C=5/9*({temp}-32)={Tc}C')
    else:
        print('输入有误,示例:1C或者1F')
if switch_type == 'L':
    temp = input('请输入长度(示例:1m或者1ft):')
    if temp.endswith('m'):
        temp = float(temp.strip('m'))
        ft = 3.281*temp
        print(f'ft=3.281*{temp}={ft}ft')
    elif temp.endswith('ft'):
        temp = float(temp.strip('ft'))
        M = 0.3048*temp
        print(f'M=3.281*{temp}={M}m')
    else:
        print('输入有误,示例:1m或者1ft')   
if switch_type == 'D':
    temp = input('请输入货币(示例:1美元或者1人民币元):')
    if temp.endswith('美元'):
        temp = float(temp.strip('美元'))
        RMB = 6.671*temp
        print(f'RMB=6.671*{temp}={RMB}人民币元')
    elif temp.endswith('人民币元'):
        temp = float(temp.strip('人民币元'))
        dollar = 0.1499*temp
        print(f'dollar=0.1499*{temp}={dollar}美元')
    else:
        print('输入有误,示例:1美元或者1人民币元')
T 温度转换
L 长度转换
D 货币转换
请输入转换类型:D
请输入货币(示例:1美元或者1人民币元):10美元
RMB=6.671*10.0=66.71000000000001人民币元

转载于:https://blog.51cto.com/13587169/2126181

  • 3
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值