首页 文章

带有输入的ValueError并尝试将输入值转换为int

提问于
浏览
0

我是初学者,所以请耐心等待 . 我试图解决一个非常简单的问题,但我得到输入和int命令的一致错误 . 我想解决的问题如下:

你有50万欧元的债务 . 您可以比较不同的存款,最赚钱的存款是每年复利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的无效文字:''

有人能告诉我为什么会收到此错误吗?我在哪里解决这个问题错了?提前致谢 .

1 回答

  • 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)
    

相关问题