这里我有以下尝试计算复杂的利息和抵押贷款利息的例子。然而,它给出了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
答案 0 :(得分:0)
前一行你的括号太多了,你可能也想要一个括号:
MonthlyPayment = FixedPayment + (
TotalAmountMortgage - (
(FixedPayment * TotalMonthsFixedInterest + AlreadyPaidAmount)
) * FloatingInterest / 1200)
----------------------^
我不得不跨越多行来说明这个问题,你可能想要做同样的事情来保持代码的可读性。
因为Python 允许在使用括号时断开多行的表达式,所以Python无法检测到忘记一个右括号直到下一行代码。这同样适用于方括号([]
)和花括号({}
)。
当你遇到语法错误时,经验法则是查看前面的行。
答案 1 :(得分:0)
你错过了这一行:
MonthlyPayment = FixedPayment+(TotalAmountMortgage-((FixedPayment*TotalMonthsFixedInterest+AlreadyPaidAmount))*FloatingInterest/1200
最后放一个关闭的人,你应该好好去。