【发布时间】:2013-07-30 11:30:14
【问题描述】:
考虑到圆形物体的直径和价格,我正在尝试计算其每平方的成本。
这是我得到的:
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()
我数学不好,所以想知道公式是否正确?
【问题讨论】:
-
我想警告您不要调用 eval(input()) - 用户可以输入一些有效的 python 代码,它将在评估过程中执行。使用 float(input()) 更安全
-
谢谢我不知道,我刚刚开始使用 Python。
-
这个问题似乎是题外话,因为它是关于数学,而不是编程。
-
数学?这是一个高中代数公式。如果你真的阅读了代码,这个问题很大程度上是一个编程问题,而不是数学问题。
-
@onimoj:关于@SteveBarnes 的疑虑:如果您曾经切换到Python2.x,请记住
input会评估自己(您必须使用raw_input来防止这种情况发生)。
标签: python math python-3.x geometry