Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

โปรแกรม Python แปลงความสูงจากเซนติเมตรเป็นฟุตและนิ้ว


แปลงความสูงจากเซนติเมตรเป็นนิ้วและฟุต โดยใช้ค่าคงที่การแปลง และปัดทศนิยม 2 ตำแหน่ง

ตัวอย่างโค้ด

cm = int(input("Enter height in cm: "))
inches = cm * 0.394
feet = cm * 0.0328

print("Height in inches:", round(inches, 2))
print("Height in feet:", round(feet, 2))

ผลลัพธ์

Enter height in cm: 178
Height in inches: 70.13
Height in feet: 5.84

คำอธิบาย

  • 1 cm = 0.393701 นิ้ว ≈ 0.394

  • 1 cm = 0.0328084 ฟุต ≈ 0.0328

  • คูณค่าที่ป้อนด้วยค่าคงที่

  • round(x, 2) ปัดเศษ 2 ตำแหน่งทศนิยม

  • 178 cm ≈ 70.13 นิ้ว ≈ 5.84 ฟุต (ประมาณ 5 ฟุต 10 นิ้ว)