python写demo报错TypeError: 'builtin_function_or_method' object is not subscriptable

本文介绍了Python写脚本时出现TypeError: ‘builtin_function_or_method’ object is not subscriptable错误的解决方法。作者在调用math.sqrt函数时出错,原因是用了[ ]而非( ),不能把函数当字典使用,还提到使用的是pycharm编辑器。

python写脚本时报错TypeError:‘builtin_function_or_method’ object is not subscriptable的解决方法

今天写一个进行python练习时,写了一个小脚本,需要调用python内置函数math中的sqrt函数,运行代码时出现错误TypeError: ‘builtin_function_or_method’ object is not subscriptable,仔细思考了一下,然后找到了解决方法。

import math

a = float(input('a = '))
b = float(input('b = '))
c = float(input('c = '))
if a + b > c and a + c > b and b + c > a:
    perimeter = a + b + c
    area = 1 / 4 * math.sqrt[(a + b + c) * (a + b - c) * (a + c - b) * (b + c - a)]
    print('周长是%.2f,面积是%.2f' % (perimeter, area))
else:
    print("不能构成三角形")

运行结果:

a = 3
b = 4
c = 5
Traceback (most recent call last):
  File "C:/Users/XXXXXX.py", line 8, in <module>
    area = 1 / 4 * math.sqrt[(a + b + c) * (a + b - c) * (a + c - b) * (b + c - a)]
TypeError: 'builtin_function_or_method' object is not subscriptable

Process finished with exit code 1

原因是内置函数math.sqrt()应该用( )而不是[ ],不能吧函数当成字典使用!
修改后:

import math

a = float(input('a = '))
b = float(input('b = '))
c = float(input('c = '))
if a + b > c and a + c > b and b + c > a:
    perimeter = a + b + c
    area = 1 / 4 * math.sqrt((a + b + c) * (a + b - c) * (a + c - b) * (b + c - a))
    print('周长是%.2f,面积是%.2f' % (perimeter, area))
else:
    print("不能构成三角形")

再次运行,运行结果:

a = 3
b = 4
c = 5
周长是12.00,面积是6.00

Process finished with exit code 0

注意:
上述我使用的时pycharm编辑器

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值