我是一名初学者,正试图通过MIT OpenCourseware学习python。此问题来自part B of problem set 1。我理解数学表达式,但我不知道如何在代码中写出来。 这就是问题: 确定将需要多长时间才能攒够 在给出以下假设的情况下,为房子支付首付款的钱:
这是一个测试用例:
输入您的起始年薪:120000 输入你的薪水,以节省的百分比,以十进制:0.05 输入您的梦想家园的费用1:50万 进入半年度加薪,为十进制:0.03 个月的数量:142
这是我的代码:
annual_salary = float(input('Annual Salary: '))
portion_saved = float(input('Portion saved (decimal): '))
total_cost = float(input('Total Cost of House: '))
semi_annual_rise = float(input('Semi-annual salary rise (decimal): '))
downp = float(0.25*total_cost)
current_savings = 0
monthly_savings = float(portion_saved*annual_salary/12)
ret = 1 + .04/12
rise = 1 + semi_annual_rise
n = 0
while current_savings < downp:
for n in range(0,300,6):
monthly_savings = monthly_savings*(rise)
current_savings = (current_savings + monthly_savings)*(ret)
n += 1
print ('Number of months: ' + str(n))
在while循环中,我试图在6,12,18等月后增加工资。但我不知道如何插入这样的条件我知道这是错的,但我不知道如何纠正它。请帮帮我!!
答案 0 :(得分:0)
当 n%6 == 0 ...
时,只需加注...
n = 0
while current_savings < downp:
n += 1
if n % 6 == 0: #every six months
monthly_savings = monthly_savings*(rise)
current_savings = (current_savings + monthly_savings)*(ret)
...