python - 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)
当我运行它们时,这两个程序都返回相同的结果,并且我一直试图弄清楚为什么以及如何自己几个小时而没有太大的改进。
解决方案
答案是通过 if 语句每 6 个月更新一次part_amount,而不是在 yearn_salary 上通过 semi_annual_raise。
感谢评论中的兰迪。
推荐阅读
- php - CodeIgniter 登录控制器表单身份验证不起作用
- amazon-web-services - AWS API Gateway 从 POSTMAN 工作,但不是从 AWS Lambda 函数
- c - 用循环填充列表的问题 - C 语言
- c++ - 如何向 task.json 添加多个包含路径?
- c - C程序没有输出
- openlayers - Openlayers - 从特定投影转换多边形,如 EPSG:28992
- javascript - Discord.js:如果用户输入特定字符串,如何将用户从任何语音通道移动到某个语音通道?
- html - Expand/Collapse sidebar by pressing it's border
- sql-server - SSMS 备份集列表 - 这里应该显示什么?
- maven - 不可解析的导入 POM:无法从/到 camunda-bpm-nexus 传输工件 org.camunda.bpm:camunda-bom:pom:7.14.0-ee