Python MIT OCW 6.0001 pset 1b,计算所需的保存月数。3个测试,第一个是正确的,其他两个比我的代码给出的多1个月,为什么?

Python MIT OCW 6.0001 pset 1b,计算所需的保存月数。3个测试,第一个是正确的,其他两个比我的代码给出的多1个月,为什么?,python,if-statement,while-loop,raise,Python,If Statement,While Loop,Raise,下面是3个测试,请帮忙 最后2个分别给出158和260作为我的代码结果 这是一种算法,根据工资、半年加薪、每月储蓄金额和投资回报来计算支付房屋首付款所需的储蓄月数。\p>\Code 年薪=浮动(输入(“你的起始年薪是多少?”) 月薪=年薪/12 partition_saved=float(输入(“您每月将节省多少百分比的工资(小数)”) 总成本=浮动(输入(“你的梦想家园是多少?”) 半年度加薪=浮动(输入(“你每6个月加薪多少(小数)?)) 部分首付=0.25*总成本 r=0.04 当前储蓄=

下面是3个测试,请帮忙 最后2个分别给出158和260作为我的代码结果 这是一种算法,根据工资、半年加薪、每月储蓄金额和投资回报来计算支付房屋首付款所需的储蓄月数。

\p>\Code

年薪=浮动(输入(“你的起始年薪是多少?”)
月薪=年薪/12
partition_saved=float(输入(“您每月将节省多少百分比的工资(小数)”)
总成本=浮动(输入(“你的梦想家园是多少?”)
半年度加薪=浮动(输入(“你每6个月加薪多少(小数)?))
部分首付=0.25*总成本
r=0.04
当前储蓄=0
月份=0
当活期存款<部分首付时:
活期储蓄+=部分储蓄*月薪+活期储蓄*r/12
月份+=1
如果月份%6==0:
月薪+=半年薪*月薪
打印('月数:',月数)

#月数+=1的错误放置导致错误

是否存在特定问题?你做过调试吗?我建议阅读。是的,我已经做了所有的事情代码没有错只是在最后两个测试中没有返回正确的值,我在while循环中放置if语句有什么问题吗?代码没有错吗?它没有在最后两个测试中返回正确的值吗?
#Code
'''

annual_salary = float(input('What is your starting annual salary?'))
monthly_salary = annual_salary/12
portion_saved = float(input('What percentage of your salary will you save each month(in decimals)?'))
Total_cost = float(input('How much is your dream home?'))
semi_annual_raise = float(input('What is your raise every 6 months(in decimals)?'))
portion_down_payment = 0.25*Total_cost
r = 0.04
current_savings = 0
months = 0
while current_savings < portion_down_payment:
    months += 1
    if months % 6 == 0:
        monthly_salary += semi_annual_raise*monthly_salary
    current_savings += portion_saved*monthly_salary + current_savings*r/12
print('Number of months:', months)
      1:Enter your starting annual salary:  120000
        Enter the percent of your salary to save, as a decimal:  .05 
        Enter the cost of     your dream home:  500000
        Enter the semi­annual raise, as a decimal:  .03
        Number of months:  142

      2:Enter your starting annual salary:  80000
        Enter the percent of your salary to save, as a decimal:  .1 
        Enter the cost of your dream home:  800000
        Enter the semi­annual raise, as a decimal:  .03
        Number of months:  159

      3:Enter your starting annual salary:  75000
        Enter the percent of your salary to save, as a decimal:  .05 
        Enter the cost of your dream home:  1500000
        Enter the semi­annual raise, as a decimal:  .05
        Number of months:  261
annual_salary = float(input('What is your starting annual salary?'))
monthly_salary = annual_salary/12
portion_saved = float(input('What percentage of your salary will you save each month(in decimals)?'))
Total_cost = float(input('How much is your dream home?'))
semi_annual_raise = float(input('What is your raise every 6 months(in decimals)?'))
portion_down_payment = 0.25*Total_cost
r = 0.04
current_savings = 0
months = 0
while current_savings < portion_down_payment:
    current_savings += portion_saved*monthly_salary + current_savings*r/12
    months += 1
    if months % 6 == 0:
        monthly_salary += semi_annual_raise*monthly_salary
print('Number of months:', months)