计算圆形对象的每平方成本

时间:2013-07-30 11:30:14

标签: python math python-3.x geometry

考虑到它的直径和价格,我试图计算圆形物体的每平方成本。

这是我得到的:

import math

def main():
    print("This program calculates the cost per square inch of a circular object.")

    diameter = eval(input("What is the diameter of the object? "))
    price = eval(input("What is the price of the whole object? "))

    cost_per_square = (math.pi * (diameter / 2)**2) / price

    print("The cost per square inch is $", round(cost_per_square, 2), sep="")

main()

我不擅长数学,所以我想知道公式是否正确?

3 个答案:

答案 0 :(得分:3)

是的,圆形区域的公式是A =π* r * r。

但是price应该在分子中,area在分母中。您已编码 - 每单位成本的平方英尺。想想你想要的单位:每平方英尺的成本。这将指导你。

我建议将diameter除以2.0而不是2来避免整数除法的问题。

答案 1 :(得分:3)

我还建议先用正确的名称计算中间值。这通常可以防止错误:

radius = diameter / 2.0
area = math.pi * radius**2
price_per_area = price / area

你也可能已经注意到我在“square”之前选择“price”而在“square”之前选择“area”。那是因为互换使用同义词也会带来错误的空间。现在所有三行都非常简单,以至于很难引入您最初的错误。

答案 2 :(得分:0)

查找圆圈区域的公式为pi*r*r。要获得每平方英寸的费用,请执行以下操作:price / area