Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
二分法搜索在我的python代码中不起作用_Python_Search_Bisection - Fatal编程技术网

二分法搜索在我的python代码中不起作用

二分法搜索在我的python代码中不起作用,python,search,bisection,Python,Search,Bisection,我试图找到最佳的储蓄率每月使用对分搜索与给定的半年加薪和投资回报率。代码在无限循环上运行 # cost of the house total_cost = 1000000 portion_down_payment = 0.25 * total_cost current_savings = 0 annual_salary = int(input("enter your starting salary: ")) starting_salary = annual_salary / 12 semi_

我试图找到最佳的储蓄率每月使用对分搜索与给定的半年加薪和投资回报率。代码在无限循环上运行

# cost of the house 
total_cost = 1000000
portion_down_payment = 0.25 * total_cost
current_savings = 0
annual_salary = int(input("enter your starting salary: "))
starting_salary = annual_salary / 12

semi_annual_raise = 0.07
# annual rate of return on investment is 4%
inv_rate = 0.04 / 12
months = 0
low = 0
high = 10000
number_steps = 0
# finding current savings using  Bisection search
while abs((current_savings*36) - portion_down_payment) >= 100:
    number_steps += 1
    starting_salary = annual_salary / 12
    for months in range(1, 37):
        current_savings += (starting_salary * inv_rate)
# semi annual raise of 7 %
        if months % 6 == 0:
            starting_salary += starting_salary * 0.07

    if current_savings * 36 < portion_down_payment:
        low = current_savings
    else:
        high = current_savings
    current_savings = (high + low) / 2
print("number of steps taken by bisection search is :", number_steps)
# printing savings rate 
print(current_savings / starting_salary)
#房子的成本
总成本=1000000
部分首付=0.25*总成本
当前储蓄=0
年薪=整数(输入(“输入您的起薪:”)
起薪=年薪/12
半年度加薪=0.07
#年投资回报率为4%
投资率=0.04/12
月份=0
低=0
高=10000
步数=0
#使用二分法搜索查找当前储蓄
而abs((当前储蓄*36)-部分首付>=100:
步数+=1
起薪=年薪/12
对于范围(1,37)内的月份:
当前储蓄+=(起始工资*投资率)
#每半年加薪7%
如果月份%6==0:
起薪+=起薪*0.07
如果当前存款*36<部分首付:
低=当前储蓄
其他:
高=当前储蓄
当前储蓄=(高+低)/2
打印(“对分搜索采取的步骤数为:”,步骤数)
#印刷储蓄率
打印(当前存款/起始工资)

您必须从头开始实现它吗?还是使用内置的
对分
是一个选项?我不能使用除loopsalso以外的其他方法,为什么您认为这是一个对分搜索?对分搜索假设为低和高,然后在两者之间找到答案需要排序输入,然后迭代地将其切成两半,移除不相关的部分,直到只剩下一个元素。我看不出这里的代码有任何相似之处。我们讨论的是同一个问题吗?你必须从头开始实现它吗?还是使用内置的
对分
是一个选项?我只能使用loopsalso,为什么你认为这是一个对分搜索?对分搜索假设低和高,然后在两者之间找到答案需要一个排序的输入,然后迭代地将其切成两半,移除不相关的部分,直到只剩下一个元素。我看不出这里的代码有任何相似之处。我们谈论的是同一个问题吗?