简体   繁体   English

计算圆形物体的每平方成本

[英]Calculate the cost per square of a circular object

I am trying to calculate the cost per square of a circular object, given its diameter and price. 考虑到直径和价格,我试图计算圆形物体的每平方成本。

Here is what I got: 这是我得到的:

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

I'm not good at math, so I wondered if the formula is correct ? 我不擅长数学,所以我想知道公式是否正确?

Yes, the formula for the area of a circle is A = π * r * r. 是的,圆的面积公式为A =π* r * r。

But price should be in the numerator and area in the denominator. 但是price应在分子和分母中的area内。 You've coded the inverse - square feet per unit cost. 您已对码进行了编码-每单位成本的平方英尺。 Think of the units you want: cost per square foot. 想想您想要的单位:每平方英尺成本。 That will guide you. 那将指导您。

I'd recommend dividing diameter by 2.0 rather than 2 to avoid issues with integer division. 我建议将diameter除以2.0而不是2,以免出现整数除法问题。

I would also suggest to compute intermediate values with proper names first. 我也建议首先使用专有名称计算中间值。 This often prevents mistakes in the first place: 首先,这通常可以防止错误:

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

You also might have noticed that I preferred "price" before "cost" and "area" before "square". 您可能还已经注意到,我更喜欢“价格”之前的“价格”和“面积”之前的“面积”。 That's because using synonyms interchangeably also introduces room for errors. 这是因为可互换使用同义词也带来了错误的余地。 All three lines now are so simple that it will be hard to introduce the error you first made. 现在,所有三行都非常简单,以至于很难引入您第一次犯的错误。

The formula to find area of circle is pi*r*r . 查找圆的面积的公式为pi*r*r To get the cost per square inch, do this: price / area 要获得每平方英寸的成本,请执行以下操作: price / area

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM