首页 > 解决方案 > ValueError 输入并尝试将输入值转换为 int

问题描述

我是初学者,所以请多多包涵。我正在尝试解决一个非常简单的问题,但是在输入和 int 命令中遇到了一致的错误。我试图解决的问题如下:

你有5万欧元的债务。您比较不同的存款,最赚钱的存款是年复利为 6% 的存款。你应该在这笔存款上投资多少钱才能在 N 年内拥有 5 万欧元?

我的代码是:

FV=50000   #future value of the deposit is 50,000 euros
I=0.06     #the interest rate of the deposit is 6%
N=input("number of months:")
N=int(N)
print(FV/(1+I)**N)
print("I should invest", FV/(1+I)**N, "euros to have", FV, " euros in", N, 
"months with interest", I)

但是内核在第三行(输入命令)之后停止运行和执行,当我手动按 Enter 获取换行符时,我得到一个 ValueError 代码,上面写着:

ValueError: int() 以 10 为底的无效文字:''

有人可以告诉我为什么会收到此错误吗?我在解决问题方面哪里错了?提前致谢。

标签: pythonjupyter-notebook

解决方案


该代码似乎工作正常。我将添加一些打印语句,这可能有助于使事情更清楚。看看这是否有帮助。

FV=50000   #future value of the deposit is 50,000 euros
I=0.06     #the interest rate of the deposit is 6%
print("I am a computer program, i am about to ask you for an input. please enter something and then press enter")
N=input("number of years:")
if N != '': #can be replaced with if N:
    print("you have entered-",N)
else:
    print("that is an empty string")
N=int(N)
print(FV/(1+I)**N)
print("I should invest", FV/(1+I)**N, "euros to have", FV, " euros in", N, 
"years with interest", I)

推荐阅读