Python TypeError:不支持的操作数类型-:'int'和'function' [英] Python TypeError: unsupported operand type(s) for -: 'int' and 'function'
问题描述
我是 Python 初学者,正在做作业.即使在研究了错误并应用了建议的修复程序之后,我仍然不断收到 TypeError: unsupported operand type(s) for -: 'int' and 'function'
.我不是在寻找任何人给我一个解决方案,但我会很感激再看一眼.我错过了一些东西,但我不知道是什么.这是我遇到问题的代码部分:
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:
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.
推荐答案
为了调用一个函数,你必须在函数名后加上括号,以及任何必需的参数.
In order to call a function you must add parens after the function name, as well as any required parameters.
这两行
minMonPay = minimumMonthlyPayment
monInt = monthlyInterest
您将函数分配给名称 minMonPay、monInt,但实际上并未调用它们.相反,您需要编写如下内容:
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 = minimumMonthlyPayment(previousBalance)
monInt = monthlyInterest(monthlyInterestRate)
这个定义
def minimumMonthlyPayment(previousbalance):
return (previousbalance * monthlyPaymentRate)
为您提供一个函数,该函数接受一个参数并将其称为 previousBalance.它与您之前在代码中创建的变量无关.其实我建议你改个名字,只会让初学者迷惑.
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.
此外,您创建的函数非常简单,每个函数只使用一次,因此删除它们并内联代码可能符合您的利益.
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
如果这样做,请不要忘记更新错误使用 minimumMonthlyPayment 函数的行.
Dont forget to update the line that incorrectly uses the minimumMonthlyPayment function if you do this.
# OLD CODE
print ('Minimum monthly payment: $ ' (round(minimumMonthlyPayment, 2)))
# NEW CODE
print ('Minimum monthly payment: $ ' (round(minMonPay, 2)))
这篇关于Python TypeError:不支持的操作数类型-:'int'和'function'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!