克转换成千克怎么算python_如何在Python中将磅转换为千克
更复杂但更灵活的版本:
units = {
'kg': ('weight', 1.),
'kilo': ('weight', 1.),
'kilogram': ('weight', 1.),
'lb': ('weight', 2.204),
'pound': ('weight', 2.204),
'tonne': ('weight', 0.001),
'carat': ('weight', 5000.),
'gram': ('weight', 1000.),
'dram': ('weight', 564.4),
'ounce': ('weight', 35.27),
'grain': ('weight', 15430.),
'm': ('distance', 1.),
'meter': ('distance', 1.),
'kilometer': ('distance', 0.001),
'km': ('distance', 0.001),
'centimeter': ('distance', 100.),
'cm': ('distance', 100.),
'meter': ('distance', 1.),
'mile': ('distance', 0.0006214),
'chain': ('distance', 0.04971),
'furlong': ('distance', 0.004971),
'league': ('distance', 0.0002071),
'foot': ('distance', 3.281),
'feet': ('distance', 3.281), # irregular plural - must be explicitly specified!
'inch': ('distance', 39.37)
}
def getUnit(unit_name):
if unit_name in units:
return units[unit_name]
# recognize regular plural forms
elif unit_name.endswith('es') and unit_name[:-2] in units:
return units[unit_name[:-2]]
elif unit_name.endswith('s') and unit_name[:-1] in units:
return units[unit_name[:-1]]
# not found?
else:
raise ValueError("Unrecognized unit '{0}'".format(unit_name))
def convert(amt, from_unit, to_unit):
typeA, numA = getUnit(from_unit)
typeB, numB = getUnit(to_unit)
if typeA==typeB:
return amt * numB / numA
else:
raise ValueError("Units are of different types ('{0}' and '{1}')".format(typeA, typeB))
def conversion(s):
"""
Do unit conversion
Accepts a string of the form
"(number) (unitA) [to] (unitB)"
If unitA and unitB are of the same unit-type, returns the converted value.
"""
s = s.strip().lower().split()
if len(s) not in (3, 4):
raise ValueError("Argument string has wrong number of words (should be three or four)")
try:
amt = float(s[0])
except ValueError:
raise ValueError("Argument string must start with a number")
from_unit = s[1]
to_unit = s[-1]
return convert(amt, from_unit, to_unit)
def tryAgain():
s = raw_input('Try again (Y/n)? ').strip().lower()
return 'yes'.startswith(s)
def main():
while True:
s = raw_input("Convert what? (ex: 10 meters to feet) ")
try:
print(": {0}".format(conversion(s)))
except ValueError, v:
print v
if not tryAgain():
break
if __name__=="__main__":
main()
这可以解决诸如10吨到盎司的问题"或者" 30弗隆到脚"。
建议进一步补充:
以距离为单位添加纳米(并测试)。
以重量单位加上短吨和石头(并测试它们)。
以体积为单位添加升,品脱和桶(并测试它们)。
醇>
克转换成千克怎么算python_如何在Python中将磅转换为千克相关推荐
- linux shell转换成时间,如何在Bash中将时间戳转换为日期?
如何在Bash中将时间戳转换为日期? 我需要一个将Unix时间戳转换为日期的shell命令或脚本. 输入可以来自第一个参数或来自stdin,允许以下使用模式: ts2date 1267619929 和 ...
- 第二章第四题(将磅转换为千克)(convert pound to kilogram)
第二章第四题(将磅转换为千克)(convert pound to kilogram) 2.4(将磅转换为千克)编写程序,将磅数转换为千克数.程序提示用户输入磅数,然后转换成千克并显示结果.1磅等于0. ...
- Java黑皮书课后题第2章:2.4(将磅转换为千克)编写程序,将磅数转换为千克数。程序提示用户输入磅数,然后转换为千克并显示结果。1磅等于0.454千克
2.4(将磅转换为千克)编写程序,将磅数转换为千克数.程序提示用户输入磅数,然后转换为千克并显示结果.1磅等于0.454千克 题目 题目概述 运行示例 破题 代码块 方法评价 修改日志 题目 题目概述 ...
- python数字形式转换_在Python中将字母转换为数字
在Python中将字母转换为数字 如何完成以下步骤? characters = ['a''b''c''d''e''f''g''h''i''j''k''l''m''n''o''p''q''r''t''u ...
- 1011:2.4将磅转换为千克
1011:2.4将磅转换为千克
- 克转换成千克怎么算python_磅到公斤和克的转换python函数
我需要创建一个名为poundstometry的python函数,它将以磅为单位的权重转换为千克和克. 例如,正确答案是2公斤200克,而不是打印出2.2公斤 为了帮助您的工作,以下转换保持: 1磅=2 ...
- pdf转换成html python,在Python中将pdf转换为html
Python 2.6 我试图解析我的pdf文件,其中一种方法是将其转换为html并提取标题和段落. 所以,我尝试了pdf2htmlEX,它将我的pdf转换成html格式,而不干扰我的pdf格式...到 ...
- python将源代码转换成在html可显示的格式,Python实现将HTML转换成doc格式文件的方法示例...
Python实现将HTML转换成doc格式文件的方法示例 来源:中文源码网 浏览: 次 日期:2018年9月2日 [下载文档: Python实现将HTML转换成doc格式文件的方法示例. ...
- python中将数字转换为字母_用python把数字转换成字母
方法一(普通方法): # -*- coding: utf-8 -*- #把数字转换成相应的字符,1-->'A' 27-->'AA' def changeNumToChar(toSmallC ...
最新文章
- Spring 4 使用Freemarker模板发送邮件添加附件
- 华为云AIOps实践全面解析
- Linux 操作系统原理 — cgroups 进程资源配额与管理
- AIX存储管理(二)物理卷管理
- 远程连接linux服务器mysql
- python如何判断字典中是否存在某个键_总结:11个Python3字典内置方法大全及示例...
- oracle jdbctype null,Oracle数据库之springboot 项目mybatis plus 设置 jdbcTypeForNull
- java生成excel文件
- java构建xml参数_Java中使用XML创建EMAIL模板
- python基础数据类型操作系统_python基础数据类型
- floyd算法_常用十大算法(九)— 弗洛伊德算法
- PDF阅读器如何实现免费论文查重
- 桌游跑团用roll点器,可以自己设置色字的数量和种类
- MySQL字符串拼接的两种方式
- java md5加密 jar包,java md5加密工具类学习示例
- 数据中心的三种布线方式(EOR/MOR/TOR)
- JAVA HD japan_电竞小金刚:优派VX2778-hd-PRO
- 现代软件工程讲义 7 设计阶段 典型用户 - 故事 - 任务 - 具体工作
- python xlsm_“xlwings”:不支持写入.xlsm文件?
- 医疗管理系统-预约管理