python - 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 为底的无效文字:''
有人可以告诉我为什么会收到此错误吗?我在解决问题方面哪里错了?提前致谢。
解决方案
该代码似乎工作正常。我将添加一些打印语句,这可能有助于使事情更清楚。看看这是否有帮助。
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)
推荐阅读
- python-3.x - 带有GUI的python中的实时互联网连接检查器
- python - 如何在不删除封闭文本的情况下从 python 3 中的 xml 文档中删除粗体标签?
- google-apps-script - 如何在 Google Apps 脚本库中创建枚举?
- selenium - 正常使用 Chrome 浏览器和通过 Selenium WebDriver 的区别
- r - 使用 read.table 读取两列数据会导致三列
- css - 更改第二个和第三个 div 的背景颜色
- yubico - 如何将钥匙移动到 Yubikey PIV 上的已停用插槽?
- perl - 如何解决失控的多行“”字符串错误?
- firefox - CORS 请求在 Firefox 中被阻止
- angular - 如何在 Angular 7 / Typescript 中使用变量类名?