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

I am starting to work in Python and am working on a task. I keep getting TypeError: unsupported operand type(s) for -: 'int' and 'function' even after investigating the error and applying the proposed fixes. I'm not looking for anyone to give 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 problems 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 all the 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 would really appreciate your understanding. Thanks.

+6
source share
3 answers

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 functions to the names minMonPay, monInt, but you don’t actually call them. Rather, you 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 its previousBalance. This has nothing to do with the variable you created earlier in your code. In fact, I suggest you rename it, it can only confuse you as a beginner.

In addition, the functions you created are so simple and used only once that it may be in your interest to delete them and embed 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 

Remember to update the line that uses the minimumMonthlyPayment function incorrectly if you do.

 # OLD CODE print ('Minimum monthly payment: $ ' (round(minimumMonthlyPayment, 2))) # NEW CODE print ('Minimum monthly payment: $ ' (round(minMonPay, 2))) 
+5
source

I think the problem in this line is minMonPay = minimumMonthlyPayment looks like you are assigning a variable to a method, rather than calling a method.

0
source

Look at your code, and it looks like you are assigning a function to a variable, and then trying to disconnect the function from the value.

You define the function "minimumMonthlyPayment (previous balance)", but then you assign the function to the variable "minMonPay = minimumMonthlyPayment". Then you say that the previous balance is minMonPay. I think you wanted to do this:

minMonPay = minimumMonthlyPayment (previous balance)

Hope this helps.

0
source

Source: https://habr.com/ru/post/927882/


All Articles