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

 时间:2026-02-16 17:07:04

1、新建一个空白的Python文档。

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

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

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

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

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

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

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

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

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

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

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

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

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

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

5、为了让程序观看方便,定义变量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使单位米转换为厘米

  • tanx的导数,tanx的导数推导
  • python中!=是什么意思?
  • arctan怎么算?
  • 反三角函数图像与性质是什么
  • 如何用python编写一个求分段函数的值的程序
  • 热门搜索
    小米耳机怎么样 牙菌斑怎么去除 郏县怎么读 脸部过敏怎么办 胃酸过多怎么办 牙齿松动怎么办 抖音怎么拍 早射怎么办 微信群怎么@所有人 沙冰怎么做