输入值出错,正在尝试将输入值转换为输入值

2024-06-07 06:48:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我是个初学者,所以请容忍我。我试图解决一个非常简单的问题,但是在input和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)

但是内核在第三行(input命令)之后停止运行和执行,当我手动按Enter获取换行符时,会得到一个ValueError代码,该代码表示:

ValueError:基数为10的int()的文本无效:“”

有人能告诉我为什么我会犯这个错误吗?我在解决问题上哪里错了?提前谢谢。你知道吗


Tags: ofthe代码命令inputis错误int
1条回答
网友
1楼 · 发布于 2024-06-07 06:48:08

代码似乎运行良好。我将添加一些打印语句,可能有助于使事情更清楚。看看这是否有用。你知道吗

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)

相关问题 更多 >