当用户输入现值、利率和年数时,计算帐户的未来价值

2024-04-25 07:42:15 发布

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

每当我运行这个时,它只会继续得到p值,而不是计算出的帐户的未来值

def main():
    p=eval(input("Enter in the present value of the account: "))
    i=eval(input("Enter in the monthly interest rate(%): "))
    I=eval(str(i//100))
    t=eval(input("Enter the number of months that that the money will be in the account: "))

    print(futureValue(p, I, t),"Is the future value of your account!")

def futureValue(p, I, t):
    return p*((1 + I) ** t)

main()

Tags: oftheininputthatvaluemaindef
1条回答
网友
1楼 · 发布于 2024-04-25 07:42:15

这是因为在i//100中使用//,而不是/。这将导致i/100的结果向下舍入,因此总是导致0.0i < 100一样长(将是这种情况)。这就是为什么你的未来价值总是和现在一样,因为你把钱放在没有利息的地方

只需更改:

I=eval(str(i//100))

分为:

I=eval(str(i/100))

另外,由于您从来都不需要评估I(它只是i/100,您已经从用户输入中获得了evali),请尝试简单地将I=i/100放在如下位置:

def main():
    p=eval(input("Enter in the present value of the account: "))
    i=eval(input("Enter in the monthly interest rate(%): "))
    I=i/100 #simply put this
    t=eval(input("Enter the number of months that that the money will be in the account: "))

    print(futureValue(p, I, t),"Is the future value of your account!")

def futureValue(p, I, t):
    return p*((1 + I) ** t)

main()

你应该得到你的未来价值

相关问题 更多 >