Python 如何让我的程序打印浮动?
我写了一个小程序来帮助我做数学作业:Python 如何让我的程序打印浮动?,python,string,math,floating-point,geometry,Python,String,Math,Floating Point,Geometry,我写了一个小程序来帮助我做数学作业: import math def saCircle(): while True: radius = float(raw_input("Enter the radius: ")) print "\nFinding area with %d as the radius" % radius x = math.pi * radius**2 print "\nThe area of your
import math
def saCircle():
while True:
radius = float(raw_input("Enter the radius: "))
print "\nFinding area with %d as the radius" % radius
x = math.pi * radius**2
print "\nThe area of your circle is %d\n" % x
saCircle()
问题是它将接受十进制数,但不会打印出十进制数的值
如何解决此问题?使用
%f
而不是%d
(将数字四舍五入为整数)打印浮点:
>>> radius = 4.4
>>> x = math.pi * radius**2
>>> print "\nThe area of your circle is %f\n" % x
The area of your circle is 60.821234
问题很好地解释了差异。浮点的格式说明符是
%f
,而不是%d
(%d
表示整数)
有关更多详细信息,请参阅上的维基百科文章。谢谢。。。我会看看的。
print "\nFinding area with %f as the radius" % radius
^