xuanxi 发表于 2018-8-6 09:29:55

案例:python实现单位转换

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人民币元')
页: [1]
查看完整版本: 案例:python实现单位转换