如何让我的程序打印浮动?

2024-04-19 18:15:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我写了一个小程序来帮助我做数学作业:

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() 

问题是它将接受十进制数,但不会打印出十进制数的值。你知道吗

我怎样才能解决这个问题?你知道吗


Tags: theimport程序truerawdef作业数学
2条回答

浮点的格式说明符是%f,而不是%d%d表示整数)。你知道吗

print "\nFinding area with %f as the radius" % radius
                           ^ 

有关详细信息,请参阅维基百科的^{} format strings文章。你知道吗

使用%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

This这个问题很好地解释了这种差异。你知道吗

相关问题 更多 >