【发布时间】:2016-06-04 17:12:06
【问题描述】:
完全新手在这里尝试学习。我应该创建一个我相信我已经在这里完成的模块:
import math
def circum(x):
return 2 * math.pi * x (format (",.3f"))
def area(x):
return math.pi * x **2 (format (",.3f"))
我已将此模块命名为 mycircle。 现在我必须导入这个模块并用它来计算圆的周长和面积。我的代码是:
import mycircle
def main():
radius = float (input ("Please enter the radius of the circle: "))
circumference = mycircle.circum (radius)
area = mycircle.area (radius)
print ("The circumference of the circle is ", format (mycircle.circum, ",.3f"), sep="")
print ("The area of the circle is ", format (mycircle.area, ",.3f"), sep="")
main()
但是,我收到错误消息:
File "C:/Users/Jameson/Desktop/COP 1000/Chapter 5/54.py", line 26, in <module>
main()
File "C:/Users/Jameson/Desktop/COP 1000/Chapter 5/54.py", line 21, in main
circumference = mycircle.circum (radius)
File "C:/Users/Jameson/Desktop/COP 1000/Chapter 5\mycircle.py", line 4, in circum
return 2 * math.pi * x (format (",.3f"))
TypeError: 'float' object is not callable
我只能假设它是愚蠢的。任何帮助将不胜感激!我被困在这里。
【问题讨论】:
-
您在
mycircle模块中使用format的方式存在问题。但是您可能不应该尝试格式化这些函数中的数字 - 只需返回数值。这样,如果需要,您可以在进一步计算中使用结果。仅在准备好输出数字时对其进行格式化。要了解有关如何使用format的更多信息,请参阅Format Specification Mini-Language 的文档。
标签: python module python-3.5 callable