Python:谁能告诉我为什么这个while循环冻结了?

时间:2015-10-24 21:23:38

标签: python-3.x while-loop

我正在尝试为课堂完成这项功课,无论我做什么,它都会在while循环中冻结。对我来说一切似乎都是对的,但我对python也很陌生。任何帮助将不胜感激。

print("This program will tell you how long it will take you to retire.")

yearlyReturns = float(input("Enter your yearly investment returns (est .05): "))

monthlyWithdraw = float(input("Enter percentage of your savings you will withdraw monthly (est .04): "))

income = float(input("Enter your monthly income: "))

percentSavings = float(input("Enter the percent of your income you save per month: "))

monthlySavings = (income * percentSavings)

expenses = (income - monthlySavings)

expensesTotal = (expenses * 12)

yearlyReturns = (yearlyReturns / 12)

totalAmount = (yearlyReturns * monthlySavings)

retire = (totalAmount * monthlyWithdraw)
months = 0
while retire < expenses:

    totalAmount = (totalAmount + monthlySavings)

    totalAmount = (totalAmount * yearlyReturns)

    months = months + 1

years = (months / 12)
print(income)

print(expenses)

print(monthlySavings)

print("It took ",months," to save enough to retire.")

print("That will be ",years," years.")

print("See what saving a little bit more would do.")

2 个答案:

答案 0 :(得分:2)

嗯,你没有在循环中更改retireexpenses,所以一旦你进入循环,就永远不会离开。

将语句retire = totalAmount * monthlyWidthdraw移动到循环中。

答案 1 :(得分:0)

您永远不会在循环体内更改退休或费用的值。