输入错误并尝试将输入值转换为int

时间:2018-11-11 17:29:08

标签: python jupyter-notebook

我是初学者,所以请多包涵。我正在尝试解决一个非常简单的问题,但是在输入和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:以10为底的int()无效文字:

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

1 个答案:

答案 0 :(得分:2)

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

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)