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=(p*(p-a)*(p-b)*(p-c))**0.5后,问题解决。

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

  • 24
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值