小编典典

Python TypeError:^:“ float”和“ int”的不受支持的操作数类型

python

我编写了一个简单的程序,该程序使用数值积分来近似确定定积分的求值。但是,当我为什么要标题出现错误时,我感到很沮丧。请记住,我已经有一年半没有接触过python了,所以我可能很想念我很想念它,但是如果您能帮助我,我仍然感激不尽:)下面是代码:

import math
def f(x):
    f=math.sqrt(1+(6*x+4)^2)
    return f


lbound=int(input("Input lower bound for the integral"))
ubound=int(input("Input upper bound for the integral"))
n=int(input("Input number of intervals"))
dx=((ubound-lbound)/n)
integral=0
for i in range(1,n):
    integral=integral+dx*f(i*dx)

print (integral)

这是IDLE在尝试运行代码时给我的完整错误报告:

Traceback (most recent call last):
  File "C:\Users\******\Desktop\integrals.py", line 13, in <module>
    integral=integral+dx*f(n*dx)
  File "C:\Users\******\Desktop\integrals.py", line 3, in f
    f=math.sqrt(1+(6*x+4)^2)
TypeError: unsupported operand type(s) for ^: 'float' and 'int'

阅读 186

收藏
2021-01-20

共1个答案

小编典典

尝试提高功率时,请使用操作数**而不是^

f=math.sqrt(1+(6*x+4)**2)
2021-01-20