Python “不支持的操作数类型 -:'int' 和 'tuple'”是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22945826/
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 02:01:58  来源:igfitidea点击:

What does "unsupported operand type(s) for -: 'int' and 'tuple'" means?

pythonscipytuples

提问by hetapuru

I got a error saying:

我收到一条错误消息:

unsupported operand type(s) for -: 'int' and 'tuple'

How do I correct it?

我该如何纠正?

from scipy import integrate
cpbar = lambda T: (3.826 - (3.979e-3)*T + 24.558e-6*T**2 - 22.733e-9*T**3 + 6.963e-12*T**4)*8.314
deltahbarCH4 = integrate.quad(cpbar,298,1000)
var = deltahbarCH4

hRPbar = hRPbar + (deltahbarCO2 + 2*deltahbarH2O - var -2*deltahbarO2)

采纳答案by Martijn Pieters

integrate.quad()returns a tuple; deltahbarCO2 + 2*deltahbarH2Ois an integer, you are trying to subtract the vartuple.

integrate.quad()返回一个元组deltahbarCO2 + 2*deltahbarH2O是一个整数,您正在尝试减去var元组。

If you wanted justthe integral yof the integrate.quad()result, use the first element of that tuple:

如果你想只是整体y中的integrate.quad()结果,使用元组的第一个元素:

var = deltahbarCH4[0]

or use tuple assignment:

或使用元组分配:

var, err = deltabarCH4

回答by merlin2011

That error means that you are trying to use the subtraction operator -between a number and a tuple.

该错误意味着您正在尝试-在数字和元组之间使用减法运算符。

Based on the documentation, you probably want: var = deltahbarCH4[0], since that will give you the actual value of the integral, which you are computing with on a later line.

根据文档,您可能需要:var = deltahbarCH4[0],因为这将为您提供积分的实际值,您将在稍后的行中使用该值进行计算。