Python TypeError:不支持的操作数类型-:'int'和'function'

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

Python TypeError: unsupported operand type(s) for -: 'int' and 'function'

pythontypeerror

提问by KMarciszewski

I am a beginner in Python and am working on an assignment. I keep getting TypeError: unsupported operand type(s) for -: 'int' and 'function'even after researching the error and applying the suggested fixes. I'm not looking for anyone to hand me a solution, but I would appreciate a second look. I'm missing something but I don't know what. This is the section of code I'm having trouble with:

我是 Python 初学者,正在做作业。TypeError: unsupported operand type(s) for -: 'int' and 'function'在研究了错误并应用了建议的修复程序后,我一直保持平衡。我不是在寻找任何人给我一个解决方案,但我会很感激再看一眼。我错过了一些东西,但我不知道是什么。这是我遇到问题的代码部分:

month = 0
interestYDT = 0
balance = int(raw_input ("Enter balance on credit card: "))
annualInterestRate = float(raw_input ("Enter annual interest rate as a decimal: "))
monthlyPaymentRate = float(raw_input ("Enter minimum monthly payment rate as a decimal: "))
previousbalance = balance
#
def monthlyInterestRate(annualInterestRate):
    return float(annualInterestRate/12)
#
if month <= 12:
    def minimumMonthlyPayment(previousbalance):
        return (previousbalance * monthlyPaymentRate)
    def monthlyInterest(monthlyInterestRate):
        return (1 + monthlyInterestRate)
    minMonPay = minimumMonthlyPayment
    monInt = monthlyInterest
    newbalance = ((previousbalance - minMonPay) * (monInt))
    interestYDT = (interestYTD + montInt)
    previousbalance = (newbalance)
    print ''
    print ('Month:' (month))
    print ('Minimum monthly payment: $ ' (round(minimumMonthlyPayment, 2)))
    print ('Remainging balance: $ ' (round(newbalance, 2)))
    print ' '
    month = (month + 1)

This is the entire error I get:

这是我得到的整个错误:

Traceback (most recent call last):
  File "C:/Users/Karla/Documents/_MIT 600X Introduction to CS and Prog/Assignments/Week2/kmarciszewski_week2_Problemset_Problem1.py", line 33, in <module>
    newbalance = ((previousbalance - minMonPay) * (monInt))
TypeError: unsupported operand type(s) for -: 'int' and 'function'

I'd really appreciate any insight. Thank you.

我真的很感激任何见解。谢谢你。

采纳答案by Johanna Larsson

In order to call a function you must add parens after the function name, as well as any required parameters.

为了调用一个函数,你必须在函数名之后添加括号,以及任何必需的参数。

In these two lines

在这两行

minMonPay = minimumMonthlyPayment
monInt = monthlyInterest

you assign the functions to the names minMonPay, monInt, but you don't actually call them. Rather, you would need to write something like:

您将函数分配给名称 minMonPay、monInt,但实际上并未调用它们。相反,您需要编写如下内容:

minMonPay = minimumMonthlyPayment(previousBalance)
monInt = monthlyInterest(monthlyInterestRate)

This definition

这个定义

def minimumMonthlyPayment(previousbalance):
    return (previousbalance * monthlyPaymentRate)

gives you a function that takes one parameter and calls it previousBalance. It has nothing to do with the variable you created earlier in your code. In fact, I suggest you rename it, it can only serve to confuse you as a beginner.

为您提供一个函数,该函数接受一个参数并将其称为 previousBalance。它与您之前在代码中创建的变量无关。其实我建议你重命名,它只会让你这个初学者感到困惑。

Further, the functions you've created are so simple, and only used once each, that it might be in your interest to remove them and inline the code.

此外,您创建的函数非常简单,每个函数只使用一次,删除它们并内联代码可能符合您的利益。

# OLD CODE
def minimumMonthlyPayment(previousbalance):
    return (previousbalance * monthlyPaymentRate)
def monthlyInterest(monthlyInterestRate):
    return (1 + monthlyInterestRate)
minMonPay = minimumMonthlyPayment
monInt = monthlyInterest

# NEW CODE
minMonPay = previousbalance * monthlyPaymentRate
monInt = 1 + monthlyInterestRate

Dont forget to update the line that incorrectly uses the minimumMonthlyPayment function if you do this.

如果您这样做,请不要忘记更新错误使用 minimumMonthlyPayment 函数的行。

# OLD CODE
print ('Minimum monthly payment: $ ' (round(minimumMonthlyPayment, 2)))

# NEW CODE
print ('Minimum monthly payment: $ ' (round(minMonPay, 2)))

回答by Trevor Pilley

I think the problem is with this line minMonPay = minimumMonthlyPaymentit looks to me like you are assigning a variable to a method rather than calling the method.

我认为问题在于这一行,minMonPay = minimumMonthlyPayment它在我看来就像您将变量分配给方法而不是调用方法一样。

回答by Mr-F

Looking at your code and it looks like you are assigning a function to a variable and then trying to minus a function from a value.

查看您的代码,看起来您正在将一个函数分配给一个变量,然后尝试从一个值中减去一个函数。

You define the function 'minimumMonthlyPayment(previousbalance)', but then you assign the function to the variable 'minMonPay = minimumMonthlyPayment'. Then later you say previousBalance - minMonPay. What I think you meant to do was:

您定义了函数“minimumMonthlyPayment(previousbalance)”,然后将该函数分配给变量“minMonPay = minimumMonthlyPayment”。然后你说previousBalance - minMonPay。我认为你的意思是:

minMonPay = minimumMonthlyPayment(previousbalance)

minMonPay = minimumMonthlyPayment(previousbalance)

Hope that helps.

希望有帮助。