Python-I';我一直在尝试每半年加薪一次(来自麻省理工学院6.0001的PSet_1b),但结果是一样的

Python-I';我一直在尝试每半年加薪一次(来自麻省理工学院6.0001的PSet_1b),但结果是一样的,python,if-statement,Python,If Statement,PSet_1a-基线 这会提示用户输入他们梦想房屋的成本、他们的工资、他们愿意节省的月薪部分,并返回他们获得首付所需的月数(他们梦想房屋成本的25%) total_cost=float(输入('梦想之家是多少?')) 部分首付=0.25 当前储蓄=0 r=0.04 年薪=浮动(输入(“你的工资是多少?”) 部分保存=浮动(输入('每月保存的金额是多少?')) 月薪=年薪/12.0 预付款金额=总成本*部分预付款 部分金额=月工资*部分保存 月份=0 虽然目前的储蓄答案是通过if报表每6个月更新部

PSet_1a-基线

这会提示用户输入他们梦想房屋的成本、他们的工资、他们愿意节省的月薪部分,并返回他们获得首付所需的月数(他们梦想房屋成本的25%)

total_cost=float(输入('梦想之家是多少?'))
部分首付=0.25
当前储蓄=0
r=0.04
年薪=浮动(输入(“你的工资是多少?”)
部分保存=浮动(输入('每月保存的金额是多少?'))
月薪=年薪/12.0
预付款金额=总成本*部分预付款
部分金额=月工资*部分保存
月份=0

虽然目前的储蓄答案是通过if报表每6个月更新部分金额,而不是通过年薪的半年度加薪


感谢评论中的Randy。

您正在更新
年薪
,但不是
部分金额
,这是您计算中实际使用的。如果我没有弄错,您在计算中没有使用
半年薪
年薪
;因此,这与第一个代码完全相同。@Randy,如果我更新部分金额,那么它将每月添加一次,而不是我计划的每6个月添加一次。您需要在更新年薪的块中更新它,以便它仅在加薪时才更新。
total_cost = float(input('How much is the dream house? ')) 
portion_down_payment = 0.25
current_savings = 0
r = 0.04
annual_salary = float(input('What is your salary? '))
portion_saved = float(input('What is the amount saved per month? '))
monthly_salary = annual_salary / 12.0

amount_down_payment = total_cost * portion_down_payment
portion_amount = monthly_salary * portion_saved
months = 0

while current_savings <= amount_down_payment:
    current_savings += portion_amount + (current_savings * (r / 12))
    months += 1
    print(current_savings, months)
total_cost = float(input('How much is the dream house? '))
portion_down_payment = 0.25
current_savings = 0
r = 0.04

annual_salary = float(input('What is your salary? ')) 
portion_saved = float(input('What is the amount saved per month? '))
semi_annual_rate = float(input('What is the rate of your semi-annual_raise? '))
monthly_salary = annual_salary / 12.0
semi_annual_raise = monthly_salary * semi_annual_rate

amount_down_payment = total_cost * portion_down_payment
portion_amount = monthly_salary * portion_saved

months = 0

while current_savings <= amount_down_payment:
    months += 1
    if months % 6 == 0:
        annual_salary += semi_annual_raise
        
    current_savings += portion_amount + (current_savings * (r / 12))
    print(current_savings, months)