Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 当用户输入现值、利率和年数时,计算账户的未来价值_Python_Function_Python 3.x_Math - Fatal编程技术网

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

Python 当用户输入现值、利率和年数时,计算账户的未来价值,python,function,python-3.x,math,Python,Function,Python 3.x,Math,每当我运行它时,它只会继续得到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

每当我运行它时,它只会继续得到
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()

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

简单更改:

I=eval(str(i//100))
进入:

此外,由于您从未真正需要评估
I
(只是
I/100
,您已经从用户输入中评估了
I
),因此请尝试简单地将
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()

你应该得到你的未来价值

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

简单更改:

I=eval(str(i//100))
进入:

此外,由于您从未真正需要评估
I
(只是
I/100
,您已经从用户输入中评估了
I
),因此请尝试简单地将
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()

你应该得到你未来的价值

因为爱这一切是神圣的,不要
eval
raw用户输入。即使我们忽略了整个“安全问题的定义”这件事,也意味着用户的小错误可能会以你无法预测或处理的方式做出完全出乎意料的事情。如果目标是转换为
int
float
decimal.decimal
,请使用它们的构造函数。如果目标是接受
int
float
或任何其他Python文本,请使用,它接受Python文本,但不接受任意代码。出于对所有这些的热爱,请不要
eval
原始用户输入。即使我们忽略了整个“安全问题的定义”这件事,也意味着用户的小错误可能会以你无法预测或处理的方式做出完全出乎意料的事情。如果目标是转换为
int
float
decimal.decimal
,请使用它们的构造函数。如果目标是接受
int
float
或任何其他Python文本,请使用,它接受Python文本,但不接受任意代码。这里的重要变化不是删除
eval
,而是使用真除法(
/
),而不是地板除法(
/
);当输入在0-99范围内时,后者在除以100时将始终产生0,前者将产生您期望的0.0-0.99
eval
是邪恶的,但它不是罪魁祸首。是的,当你刚刚发表评论时,我正在键入更新信息。:)这里的重要变化不是删除
eval
,而是使用真正的分区(
/
)而不是楼层分区(
/
);当输入在0-99范围内时,后者在除以100时将始终产生0,前者将产生您期望的0.0-0.99
eval
是邪恶的,但它不是罪魁祸首。是的,当你刚刚发表评论时,我正在键入更新信息。:)