Python 类型错误:不支持 ^ 的操作数类型:'float' 和 'int'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34258537/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 14:42:41  来源:igfitidea点击:

Python TypeError: unsupported operand type(s) for ^: 'float' and 'int'

pythontypeerror

提问by Aris Pap

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:

我写了一个简单的程序,它使用数值积分来近似计算定积分。但是,当谈到为什么我在标题中收到错误时,我很难过。请记住,我已经有一年半没有接触过 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)

Here is the full error report IDLE gives me when trying to run the code:

这是 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'

采纳答案by ilyas patanam

When trying to raise to a power use the operand use **and not ^.

当试图提高到权力使用操作数使用**而不是^.

f=math.sqrt(1+(6*x+4)**2)