1. 程式人生 > >TypeError: 'float' object is not callable

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後,問題解決。

這應該是新手比較容易犯的錯誤,程式碼中乘法一定要用*,共勉。