创建和使用模块 "float object not callable" -Python 3.5.1
Creating and using module "float object not callable" -Python 3.5.1
完全新手在此学习。我应该创建一个我相信我已经在这里完成的模块:
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
我只能假设它是愚蠢的。任何帮助将不胜感激!我被困在这里。
2 * math.pi * x
是一个float
。当后面跟着一对 '()'
时,python 认为你在像函数一样调用它,但 float
是不可调用的。因此错误。
要修复它,该行(以及您的 area
函数中的类似行)应更改为
return format(2 * math.pi * x, ",.3f")
另外,请注意您进行了两次格式化:一次在 circum
和 area
,然后在打印时在 main
。你只需要做一次。让 circum
和 area
简单地 return 浮动更有意义,并且仅在打印它们时格式化值(在 main
中)。
编辑:
两个print
调用也是错误的:
print ("The circumference of the circle is ", format(mycircle.circum, ",.3f"), sep="")
print ("The area of the circle is ", format(mycircle.area, ",.3f"), sep="")
mycircle.circum
和mycircle.area
是函数,不是你在main
中计算的周长和面积。这些应该改为
print ("The circumference of the circle is ", format(circumference, ",.3f"), sep="")
print ("The area of the circle is ", format(area, ",.3f"), sep="")
完全新手在此学习。我应该创建一个我相信我已经在这里完成的模块:
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
我只能假设它是愚蠢的。任何帮助将不胜感激!我被困在这里。
2 * math.pi * x
是一个float
。当后面跟着一对 '()'
时,python 认为你在像函数一样调用它,但 float
是不可调用的。因此错误。
要修复它,该行(以及您的 area
函数中的类似行)应更改为
return format(2 * math.pi * x, ",.3f")
另外,请注意您进行了两次格式化:一次在 circum
和 area
,然后在打印时在 main
。你只需要做一次。让 circum
和 area
简单地 return 浮动更有意义,并且仅在打印它们时格式化值(在 main
中)。
编辑:
两个print
调用也是错误的:
print ("The circumference of the circle is ", format(mycircle.circum, ",.3f"), sep="")
print ("The area of the circle is ", format(mycircle.area, ",.3f"), sep="")
mycircle.circum
和mycircle.area
是函数,不是你在main
中计算的周长和面积。这些应该改为
print ("The circumference of the circle is ", format(circumference, ",.3f"), sep="")
print ("The area of the circle is ", format(area, ",.3f"), sep="")