gpt4 book ai didi

Python 类型错误 : unsupported operand type(s) for -: 'int' and 'function'

转载 作者:行者123 更新时间:2023-12-03 02:28:23 26 4
gpt4 key购买 nike

我是 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)

这是我得到的整个错误:

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'

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

最佳答案

为了调用函数,您必须在函数名称以及任何必需的参数后面添加括号。

在这两行中

minMonPay = minimumMonthlyPayment
monInt = monthlyInterest

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

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

这个定义

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

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

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

# 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 函数的行。

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

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

关于Python 类型错误 : unsupported operand type(s) for -: 'int' and 'function' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12923264/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com