首页 > 解决方案 > Python - 我一直在尝试增加半年加薪(来自 MIT 6.0001 的 PSet_1b),但结果是一样的

问题描述

PSet_1a - 基线

这会提示用户他们梦想中的房子的成本,他们的工资,他们愿意节省的月工资部分,并返回他们需要首付的月数(成本的 25%他们梦想中的房子)

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)

PSet_1b - 调整为半年加薪

提示用户与上述相同,但这个要求半年费率,将其变成一个数字以添加到年度加薪中。

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)

当我运行它们时,这两个程序都返回相同的结果,并且我一直试图弄清楚为什么以及如何自己几个小时而没有太大的改进。

标签: pythonif-statement

解决方案


答案是通过 if 语句每 6 个月更新一次part_amount,而不是在 yearn_salary 上通过 semi_annual_raise。

感谢评论中的兰迪。


推荐阅读