首页后端开发Pythonpython怎么输入带单位的数

python怎么输入带单位的数

时间2023-06-07 23:56:01发布访客分类Python浏览424
导读:python怎么输入带单位的数?新建一个空白的python文档。如何用Python使单位米转换为厘米首先初步输入代码要求计算机提示用户输入数字,并且会自动转换为里面。但是这个是错误的,因为metres * 100不是字符串。metres =...

python怎么输入带单位的数?

新建一个空白的python文档。

如何用Python使单位米转换为厘米

首先初步输入代码要求计算机提示用户输入数字,并且会自动转换为里面。

但是这个是错误的,因为metres * 100不是字符串。

metres = input("Please enter the metres that you want to transfer into centimetres:")

print(metres + "m" + "=" + metres * 100 + "cm")

如何用Python使单位米转换为厘米

加了STR后,还是错误,因为METRES是数字,数字要和数字相乘。

metres = input("Please enter the metres that you want to transfer into centimetres:")

print(metres + "m" + "=" + str(metres * 100) + "cm")

如何用Python使单位米转换为厘米

metres = input("Please enter the metres that you want to transfer into centimetres:")

print(metres + "m" + "=" + str(float(metres) * 100) + "cm")

这个才是正确的,可以键入数字检查是否可以执行。

如何用Python使单位米转换为厘米

为了让程序观看方便,定义变量cm。

metres = input("Please enter the metres that you want to transfer into centimetres:")

cm = float(metres) * 100

print(metres + " m " + " = " + str(cm) + " cm "

如何用Python使单位米转换为厘米

6

也可以用.format(),这样修改起来会非常方便。

metres = input("Please enter the metres that you want to transfer into centimetres:")

cm = float(metres) * 100

print("{ } m = { } cm" .format(metres, cm))

如何用Python使单位米转换为厘米

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: python怎么输入带单位的数
本文地址: https://pptw.com/jishu/66089.html
python课后测试题及答案 计算机二级考选择题吗

游客 回复需填写必要信息