Python 程式讀取身高(釐米)並將其轉換為英尺和英寸


當需要讀取以“釐米 (cm)”為單位的身高並將其轉換為“英尺”和“英寸”時,可以使用“round”方法。

以下是演示示例 −

示例

 動態演示

in_cm=int(input("Enter the height in centimeters..."))
in_inches=0.394*in_cm
in_feet=0.0328*in_cm
print("The length in inches is ")
print(round(in_inches,2))
print("The length in feet is")
print(round(in_feet,2))

輸出

Enter the height in centimeters...178
The length in inches is
70.13
The length in feet is
5.84

說明

  • 使用者輸入為“釐米 (cm)”。

  • 將其乘以 0.394,可以轉換成英寸。

  • 將其賦值給變數。

  • 將其乘以 0.0328,可以轉換成英尺。

  • 將其賦值給變數。

  • 將其四捨五入到最近的兩位小數。

  • 這兩種轉換值都在控制檯上以輸出的形式顯示。

更新時間:2021-04-16

3K+ 次瀏覽

開始 職業

完成課程後獲得認證

開始
廣告
© . All rights reserved.