TypeError: 'float' object is not callable

今天在做一道Python练习题时遇到的问题,记录一下:

请输入三个整数a,b,c,判断能否以它们为三个边长构成三角形。若能,输出YES和面积,否则输出NO

刚开始写的代码如下:

a=int(input('请输入一个整数:'))
b=int(input('请输入一个整数:'))
c=int(input('请输入一个整数:'))
if a>0 and b>0 and c>0: #判断边长大于0
    if a+b>c and b+c>a and a+c>b: #两边之和要大于第三边
        p=(a+b+c)/2
        area=(p(p-a)(p-b)(p-c))**0.5 #海伦公式求面积
    print('YES')
    print('area=%0.2f'%area)
else:
        print('NO')

运行时输入a,b,c三个整数后一直报错 TypeError: 'float' object is not callable

TypeError                                 Traceback (most recent call last)
<ipython-input-7-f185e7350a43> in <module>()
      5     if a+b>c and b+c>a and a+c>b: #两边之和要大于第三边
      6         p=(a+b+c)/2
----> 7         area=(p(p-a)(p-b)(p-c))**0.5 #海伦公式求面积
      8     print('YES')
      9     print('area=%0.2f'%area)

TypeError: 'float' object is not callable

刚开始以为是area这个名字的问题,把名字修改后还是同样的报错,调试半天突然发现该行代码中缺少星号,将代码修改为area=(p*(p-a)*(p-b)*(p-c))**0.5后,问题解决。

这应该是新手比较容易犯的错误,代码中乘法一定要用*,共勉。

猜你喜欢

转载自blog.csdn.net/xavier_muse/article/details/83316787