Python对分搜索混淆

Python对分搜索混淆,python,Python,我正试图用Python问题集1编写麻省理工学院CS简介 问题是(第3页底部): 这是我的密码: #best savings rate in 3 years annual_salary = float(input("Enter your annual salary: ")) total_cost = 1000000 semi_annual_raise = 0.07 portion_down_payment = 0.25*total_cost r = 0.04 low = 0 high = 1

我正试图用Python问题集1编写麻省理工学院CS简介

问题是(第3页底部):

这是我的密码:

#best savings rate in 3 years

annual_salary = float(input("Enter your annual salary: "))
total_cost = 1000000
semi_annual_raise = 0.07
portion_down_payment = 0.25*total_cost
r = 0.04

low = 0
high = 10000
pre_saved = int((high+low)/2)
steps = 0
current_savings = 0
epsilon = 100


months = 0
salary_increase_months = 0
portion_saved = pre_saved/10000
print(portion_saved)
while abs(current_savings - portion_down_payment) >= epsilon:

    #calculates savings after 36 months
    while(months < 36):
        #checks every 6th iteration to add the semi annual raise
        if(salary_increase_months == 6):
            annual_salary += annual_salary*semi_annual_raise
            salary_increase_months = 0
        current_savings += current_savings*(r/12) + portion_saved*(annual_salary/12)
        salary_increase_months += 1
        months += 1
        #print(current_savings)

    #bisection search portion
    if current_savings < portion_down_payment - epsilon:
        #print('1')
        low = portion_saved*10000
        current_savings = 0
    elif current_savings > portion_down_payment + epsilon:
        #print('2')
        high = portion_saved*10000
        current_savings = 0

    elif current_savings < portion_down_payment + epsilon and current_savings > portion_down_payment - epsilon:
        print('Best savings rate:', portion_saved)
        print('Steps in bisection search:', steps)
        break
    else:
        print('It is not possible to pay the down payment in three years.')
        break
    portion_saved = int((low + high)/2)
    portion_saved = portion_saved/10000
    steps += 1
    months = 0
    salary_increase_months = 0      
#三年内最佳储蓄率
年薪=浮动(输入(“输入您的年薪:”)
总成本=1000000
半年度加薪=0.07
部分首付=0.25*总成本
r=0.04
低=0
高=10000
预保存=整数((高+低)/2)
步数=0
当前储蓄=0
ε=100
月份=0
工资\加薪\月数=0
部分保存=预保存/10000
打印(已保存部分)
而abs(当前储蓄-部分首付)>=ε:
#计算36个月后的储蓄
而(月数<36):
#每6次迭代检查一次,以添加半年加薪
如果(加薪月数=6):
年薪+=年薪*半年薪
工资\加薪\月数=0
活期储蓄+=活期储蓄*(r/12)+部分储蓄*(年薪/12)
工资_增加_个月+=1
月份+=1
#打印(活期存款)
#对分搜索部分
如果当前储蓄<部分首付-ε:
#打印('1')
低=保存的部分*10000
当前储蓄=0
elif活期储蓄>部分首付+ε:
#打印('2')
高=保存的部分*10000
当前储蓄=0
elif活期存款<部分首付+ε和活期存款>部分首付-ε:
打印('最佳节省率:',已保存部分)
打印('对分搜索中的步骤:',步骤)
打破
其他:
打印('三年内无法支付首付款')
打破
保存的部分=整数((低+高)/2)
保存部分=保存部分/10000
步数+=1
月份=0
工资\加薪\月数=0

当我为工资输入一个值时,我得到的只是原始的保存百分比(0.5),并且输出没有结束。其他的都不会被打印出来。我创建了三个if语句,用来检查保存的部分的值并进行二等分搜索(如果值高于部分首付加epsilon,则high成为保存的部分,反之亦然)

您的
while months<36:
循环仅在外部
while
循环的第一次迭代时正确工作-在后续迭代中,
months
已经是36。您在
加薪\u月数
方面也有类似问题-它只相当于6次,因为您从未重置它。我添加了两行新行,重置月数和加薪月数。我仍然遇到同样的问题。除了前面提到的其他值之外,我从未重置年薪。这就是问题所在。我认为它现在起作用了。所以我现在要处理的问题是,当前的储蓄永远无法达到部分首付的价值。我以为else声明涵盖了这种情况,但实际情况是,它陷入了寻找最佳储蓄率的无限循环中。解决此问题的最佳方法是什么?我是否使用步骤数来确定是否不可能获得足够的储蓄?或者有其他方法可以做到这一点吗?在堆栈溢出问题上,不做任何解释就发布代码并没有什么帮助,也不鼓励这样做,因为它没有解释为什么或者如何回答这个问题。请提高你的文章质量。另请参见以供参考。关于代码中所需步骤的详细说明,请参见提出问题的jp207发布的习题集C部分。我想这就够了。如果需要的话,我总是可以多加几句。最好是答案(和问题)独立于外部资源;否则,当这些资源变得不可用时,就会丢失重要的上下文(这种情况经常发生)。(当然,把它们作为参考文献也很好)。此外,为了澄清,包括对代码的解释意味着解释代码如何回答OP的问题。虽然这段代码可能是一个完整的解决方案,但它并没有解释OP在发布的代码中犯了什么错误,或者他如何修复自己的特定问题。
def curr_saving(salary, Semi_raise, saveP, month):
saving = 0
monthly_income = salary/12
# down_pay = 0.25*cost
for i in range(1, month):
    if i % 6 == 0:
        salary += salary*Semi_raise
        monthly_income = salary/12
    interest = saving*(1+0.04/12)
    saving = monthly_income*saveP+interest
return saving

def Hunting_C(base, Semi_raise, cost):
    # saving = 0
    # salary = base
    # monthly_income = salary/12
    down_pay = 0.25*cost
    No_month = 36
    # saving_range = numpy.arange(0, 1, 0.0001)
    step = 0
    min_S = 0
    max_S = 1
    saveP = (min_S+max_S)/2
    while (abs(curr_saving(base, Semi_raise, saveP, No_month)-down_pay)) >= 100:
        step += 1
        if curr_saving(base, Semi_raise, saveP, No_month) > down_pay:
            max_S = saveP
        else:
            min_S = saveP
        saveP = (min_S+max_S)/2

    return f'saving rate: {saveP}, No of steps: {step}, current saving: {curr_saving(base, Semi_raise, saveP, No_month)}'


print(Hunting_C(150000, 0.07, 1000000))