2013-04-13 41 views
-1

这里我有以下例子来计算复杂的兴趣和抵押利息。然而,它给了我TotalMonthsMortgage = TotalMonthsMortgage - 1.0标记第一个TotalMonthsMortgage /左边一个的语法错误,如果我引用它,它将标记下一行AlreadyPaidAmount = AlreadyPaidAmount + TotalAmountMortgage/TotalMonthsMortgage标记左侧的AlreadyPaidAmount。我自己找不到错误,我做错了什么?Python中的语法错误试图计算复杂的兴趣

## 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 

回答

0

你有太多的括号前行,你可能想要一个在太底:

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

我必须打破跨多个线的线条来说明这个问题,你可能希望至少要保持代码的可读性。

因为Python 允许您使用括号时打破跨多个系中的表达,Python不能检测时忘记闭括号直到下一行代码。这同样适用于方括号([])和花括号({})。

因此,我们的经验法则是在之前查看行,当遇到语法错误时。

+0

感谢Martijn Pieters,解决了这个难题。 – Pythonfella

0

你缺少在这一行的括号:

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

放在最后一个右括号,你应该是好去。

+0

感谢ecline6,解决了这个难题。 – Pythonfella