我在Ex-5学习练习中正在艰难地学习Python,>
尝试编写一些将英寸和磅转换为厘米和公斤的变量。不要只输入测量值。用Python计算数学。
到目前为止,我已经做到了:
inches = 1 centimeters = 1 convert = centimeters * 2.54 print inches print centimeters print "1 inch is %s centimeters." % convert
现在将显示1英寸的转换,我将如何对其进行更改,以便用户输入以英寸或厘米为单位的金额,并且可以正确显示转换?
而且我是否正确地认为,要成功地转换值,我将不得不手动输入值,或者是否有办法在Python中完成此操作?
[我正在艰苦学习Python,在Ex-5学习练习中,它指出,尝试编写一些将英寸和磅转换为厘米和公斤的变量。不要只输入...
您可以使用词典查找指定的单位:
正确的方法是对单位使用pint或类似的库:
您可以使用input()
方法(包装在float()
方法中以将数据转换为整数
name = "Bob Jones"
age = 39
height = 74 # in inches
height_in_cm = height * 2.54
weight = 220 # in pounds
weight_in_kg = weight * 0.453592
eyes = "blue"
teeth ="white"
hair = "light brown"
print(f"Let's talk about {name}.")
print(f"He's {height} inches tall which is roughly {round(height_in_cm)} centimeters.")
print(f"He weighs {weight} pounds which is about {round(weight_in_kg)} kilograms.")
print(f"Actually, that's his target weight after six months of intermittent fasting.")
print(f"He's got {eyes} eyes and {hair} hair.")
print(f"His teeth are usually {teeth} depending on how much coffee he drinks.")