Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 784
KMarciszewski Avatar asked Oct 16 '12 20:10

KMarciszewski


People also ask

How do I fix unsupported operand type in Python?

The Python "TypeError: unsupported operand type(s) for /: 'str' and 'int'" occurs when we try to use the division / operator with a string and a number. To solve the error, convert the string to an int or a float , e.g. int(my_str) / my_num .

How do you fix unsupported operand type S for NoneType and int?

The Python "TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'" occurs when we try to use the addition (+) operator with a None value. To solve the error, figure out where the variable got assigned a None value and correct the assignment.

What does unsupported operand type S for +: int and STR mean?

The Python "TypeError: unsupported operand type(s) for +: 'int' and 'str'" occurs when we try to use the addition (+) operator with an integer and a string. To solve the error, convert the string to an integer, e.g. my_int + int(my_str) .

What does unsupported operand type S for +: int and list?

The Python "TypeError: unsupported operand type(s) for +: 'int' and 'list'" occurs when we try to use the addition (+) operator with a number and a list. To solve the error, figure out where the variable got assigned a list and correct the assignment, or access a specific value in the list.


Video Answer


1 Answers

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)))
like image 177
Johanna Larsson Avatar answered Nov 05 '22 20:11

Johanna Larsson