Python试图计算复杂兴趣时出现语法错误

2024-04-23 11:46:24 发布

您现在位置:Python中文网/ 问答频道 /正文

这里我有以下的尝试来计算复杂的利息和抵押贷款利息为例。但是,它在totalMonthMortgage=totalMonthMortgage-1.0标记第一个totalMonthMortgage/左边的一个/上给了我语法错误,如果我引用它,它会标记下一行AlreadyPaidAmount=AlreadyPaidAmount+TotalAmountMortgage/totalMonthMortgage,标记左边的AlreadyPaidAmount。当我自己找不到错误时,我做错了什么?你知道吗

## Complicated interest is the bank interest for example charged for mortgage loans

TotalAmountMortgage = float(raw_input('Enter the total amount of the mortgage to be taken:')) ##this is Principal
TotalYearsMortgage = float(raw_input('Enter the number of total years of the mortgage to be taken:'))
TotalMonthsMortgage = float(TotalYearsMortgage*12.0)
TotalYearsFixedInterest = float(raw_input('Enter the number of years with fixed interest mortgage to be taken:'))
TotalMonthsFixedInterest = 12.0*TotalYearsFixedInterest
FixedInterest = float(raw_input('Enter fixed interest for the mortgage to be taken:'))
FloatingInterest =  float(raw_input('Enter floating interest for the mortgage to be taken:'))
PoolInterestPaid = 0.0
MonthlyPayment = 0.0
AlreadyPaidAmount = 0.0
FixedPayment = float(TotalAmountMortgage/TotalMonthsMortgage)
TotalPayment = float
while (TotalMonthsMortgage-TotalMonthsFixedInterest)>0:
   MonthlyPayment = FixedPayment+(TotalAmountMortgage-((FixedPayment*TotalMonthsFixedInterest+AlreadyPaidAmount))*FloatingInterest/1200
   TotalMonthsMortgage = TotalMonthsMortgage - 1.0
   AlreadyPaidAmount = AlreadyPaidAmount+TotalAmountMortgage/TotalMonthsMortgage
TotalPayment = (TotalAmountMortgage*FixedInterest*TotalMonthsFixedInterest)/TotalMonthsMortgage+(TotalAmountMortgage*TotalMonthsFixedInterest)/TotalMonthsMortgage+PoolInterestPaid
print TotalPayment                                              ##This is the total amount to be paid
print (TotalPayment - TotalAmountMortgage)                      ##This is the amount of intererst to be paid over time
print (TotalPayment - TotalAmountMortgage)/TotalMonthsMortgage  ##This is the amount of monthly payment

Tags: ofthetoinputrawisbefloat
2条回答

你在前一行有太多的括号,你可能也想在结尾加一个:

MonthlyPayment = FixedPayment + (
    TotalAmountMortgage - (
         (FixedPayment * TotalMonthsFixedInterest + AlreadyPaidAmount)
    ) * FloatingInterest / 1200)
                    ^

为了说明这个问题,我不得不将这行代码分成多行,您可能希望这样做以保持代码的可读性。你知道吗

因为Python允许您在使用括号时跨多行打断表达式,所以Python在下一行代码之前无法检测您何时忘记了右括号。这同样适用于方括号([])和大括号({})。你知道吗

因此,当遇到语法错误时,经验法则是查看前面的行。你知道吗

此行缺少paren:

 MonthlyPayment = FixedPayment+(TotalAmountMortgage-((FixedPayment*TotalMonthsFixedInterest+AlreadyPaidAmount))*FloatingInterest/1200

最后再仔细考虑一下,你就可以走了。你知道吗

相关问题 更多 >