简体   繁体   中英

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.

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 = 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.

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.

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

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

我认为问题在于这行minMonPay = minimumMonthlyPayment它看起来像是在为方法分配变量而不是调用方法。

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:

minMonPay = minimumMonthlyPayment(previousbalance)

Hope that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM