Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 然而,如果我在循环中用一个小于1的数字乘以当前的节省值,循环就不能正常工作_Python 3.x - Fatal编程技术网

Python 3.x 然而,如果我在循环中用一个小于1的数字乘以当前的节省值,循环就不能正常工作

Python 3.x 然而,如果我在循环中用一个小于1的数字乘以当前的节省值,循环就不能正常工作,python-3.x,Python 3.x,当我输入以下代码时,我遇到了一个无限while循环。只有当我将当前储蓄乘以一个小于1的数字时,问题才会出现。099999没有给出错误,但是0.999给出了错误。我做错了什么 我尝试过将当前节省的钱与任何大于1的浮点数相乘,代码运行良好 总成本=浮动输入“输入您梦想房屋的成本:” 年薪=浮动输入'输入年薪:' 部分保存=浮动输入“输入要保存的月薪百分比,以小数为单位:” 月薪=年薪/12 已保存部分=月工资*已保存部分 部分首付=0.25*总成本 月份=0 当前储蓄=0.0 部分首付>活期储蓄:

当我输入以下代码时,我遇到了一个无限while循环。只有当我将当前储蓄乘以一个小于1的数字时,问题才会出现。099999没有给出错误,但是0.999给出了错误。我做错了什么

我尝试过将当前节省的钱与任何大于1的浮点数相乘,代码运行良好

总成本=浮动输入“输入您梦想房屋的成本:” 年薪=浮动输入'输入年薪:' 部分保存=浮动输入“输入要保存的月薪百分比,以小数为单位:” 月薪=年薪/12 已保存部分=月工资*已保存部分 部分首付=0.25*总成本 月份=0 当前储蓄=0.0 部分首付>活期储蓄: 活期存款=活期存款*0.04/12+已存部分 月数=月数+1 印刷月份
我希望编程按预期打印月数,但循环不会因为某种原因终止。

当您计算当前的储蓄时,您没有包括您已经拥有的储蓄:

current_savings = (current_savings*0.04)/12 + saved_portion
应该是

current_savings = current_savings + (current_savings*0.04)/12 + saved_portion
这就产生了新的代码:

total_cost = float(input('enter cost of your dream house: '))
annual_salary = float(input('enter annual salary: '))
portion_saved = float(input('enter percentage of monthly salary to be saved in decimals: '))
monthly_salary = annual_salary/12
saved_portion = monthly_salary*portion_saved
portion_down_payment = 0.25*total_cost
months = 0
current_savings = 0.0

while (portion_down_payment > current_savings):
    old_current = current_savings
    current_savings = current_savings + (current_savings*0.04)/12 + saved_portion
    months = months + 1  

print(months)
在while循环中打印出月份和当前的\u节省使我能够看到当前的\u节省在一个点之后停止更改。

在更新后,在循环中添加当前\u节省的打印,并查看发生了什么