刚开始学习Python。为什么当我在PowerShell中运行此程序时,结果是第9行和第10行没有浮动数字?
height = 74.0 #inches
weight = 180.0 #lbs
#converts from inches to cm/ and from lbs to kg.
cm = height * 2.5400
kg = weight * 0.453592
print ""
print "Height %d inches in cm is %d " % (height, cm)
print "Weight in %d lbs in kg is %d " % (weight, kg)
print type(kg)
print type(cm)
答案 0 :(得分:2)
您使用了%d
,用于打印整数。它将浮点数转换为整数。
我建议使用format
方法而不是%
:
print 'Height {} inches in cm is {}'.format(height, cm)
尽管使用%
:
print "Height %s inches in cm is %s" % (height, cm)
%s
在参数上调用str
。