M = L [i(1 + i)k] / [(1 + i)k-1] SyntaxError:语法无效

时间:2015-06-25 13:13:17

标签: python python-2.7

贷款计算器

M = 0

L = raw_input("What is the loan amount you took? ")

i = raw_input("What is the interest for the loan amount? ")

k = raw_input("No of payments or years you are considering for this loan? ")

M = L[i(1+i)k] / [(1+i)k-1]

print("Your monthly payment is %f" %M) 

错误:

M = L[i(1+i)k] / [(1+i)k-1]
            ^
SyntaxError: invalid syntax

2 个答案:

答案 0 :(得分:1)

无论您的输入是字符串还是数字,对于python中的乘法,您都必须在值之间指定*

如果您希望输入仅为数字或将input()转换为int为raw_input()

,则应使用int(raw_input())

您未在

处收到错误的原因
L[i(1+i)k] / [(1+i)k-1]  
   ^

是因为python认为i(1+i)是一个函数调用。

答案 1 :(得分:0)

我认为它应该是这样的。虽然如果你希望k减少然后乘以,我感到很困惑:

M = (L * (i * (1+i) * k)) / ((i+1) * (k-1))

或要递减的分母:

M = (L * (i * (1+i) * k)) / ((i+1) * k) -1)

此外,您不需要声明M = 0,因为它没有用处。