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.99eval
是邪恶的,但它不是罪魁祸首。是的,当你刚刚发表评论时,我正在键入更新信息。:)这里的重要变化不是删除eval
,而是使用真正的分区(/
)而不是楼层分区(/
);当输入在0-99范围内时,后者在除以100时将始终产生0,前者将产生您期望的0.0-0.99eval
是邪恶的,但它不是罪魁祸首。是的,当你刚刚发表评论时,我正在键入更新信息。:)