-1

在这里,我有以下尝试以计算复杂的利息和抵押贷款利息为例。但是,它在 TotalMonthsMortgage = TotalMonthsMortgage - 1.0 上给出了语法错误,标记第一个 TotalMonthsMortgage / 左侧的那个 / 如果我引用它,它标记下一行 AlreadyPaidAmount = AlreadyPaidAmount+TotalAmountMortgage/TotalMonthsMortgage 标记左侧的 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
4

2 回答 2

0

前一行的括号太多,最后可能也需要一个:

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

为了说明问题,我不得不将这条线分成多行,您可能希望这样做以至少使您的代码具有可读性。

因为 Python允许您在使用括号时将表达式拆分为多行,所以 Python 无法检测到您何时忘记了右括号,直到下一行代码。这同样适用于方括号 ( []) 和花括号 ( {})。

因此,当您遇到语法错误时,经验法则是也查看前一行。

于 2013-04-13T17:23:47.483 回答
0

您在这一行缺少一个括号:

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

最后放一个近乎的括号,你应该很高兴。

于 2013-04-13T17:24:10.213 回答