Python 类型错误:不支持 ^ 的操作数类型:'float' 和 'int' [英] Python TypeError: unsupported operand type(s) for ^: 'float' and 'int'
问题描述
我编写了一个简单的程序,它使用数值积分近似计算定积分.但是,当谈到为什么我在标题中收到错误时,我很难过.请记住,我已经有一年半没有接触过 python,所以我遗漏了一些非常明显的东西,但是如果你能帮助我,我仍然会很感激:) 这是代码:>
I wrote a simple program which approximates the evaluation of a definite integral using numerical integration. However, I am stumped when it comes to why I am getting the error in the title. Keep in mind that I haven't touched python in one and a half years so it might be something incredibly obvious that I'm missing, however I'd still be grateful if you could help me :) Here is the code:
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 在尝试运行代码时给我的完整错误报告:
Here is the full error report IDLE gives me when trying to run the code:
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'
推荐答案
当尝试提升到幂时,使用操作数使用 **
而不是 ^
.>
When trying to raise to a power use the operand use **
and not ^
.
f=math.sqrt(1+(6*x+4)**2)
这篇关于Python 类型错误:不支持 ^ 的操作数类型:'float' 和 'int'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!