简体   繁体   中英

why does this keep looping? despite me updating the appropriate variables

I reviewed this code many, many times but I can't figure out what the hell is wrong? this code is supposed to ask for user input (price of house, annual salary, semi-annual raise, number of months you're willing to wait to buy a house) then outputs what the best savings rate is to hit the target number of months. also savings gain interest at a 4% rate. A bisection search is used.

print('what is the cost of your dream home?')
total_cost=float(input())
print ('what is your annual salary?')
annual_salary=float(input())
portion_down_payment=0.25*total_cost
current_savings=0
print('enter a semi-annual raise, as a decimal')
semi_annual_raise=float(input())

print('in how mnay months do you want to buy your house?')
target_months = int(input())

number_of_months=0
saving_rate=0
first=0
last=10000
steps_in_bisection=0

while target_months != number_of_months:
    saving_rate= int((first+last)/2)
    print ('ok')
    print(number_of_months)
    current_savings=0
    number_of_months=0
    while current_savings < portion_down_payment or current_savings-100< portion_down_payment :
        current_savings += ((saving_rate/10000)*(annual_salary/12))+ (current_savings*0.04)/12
        number_of_months=1+number_of_months
        if number_of_months %6 == 0:
            annual_salary+= semi_annual_raise * annual_salary
       
    
    if number_of_months>target_months:
        first=saving_rate
        steps_in_bisection+=1
        print ('here')
        print(number_of_months)
    elif number_of_months<target_months:
        last=saving_rate
        steps_in_bisection+=1
        print('there') 
        print(number_of_months)
print("best savings rate: ", saving_rate)




print("number of months:",number_of_months)
  1. Break your code into functions, which you can then test one as a time. It is much easier to find a problem in sort piece of code than is a long one.

For example, make inner loop a function, then make outer loop a function, which will call the inner loop.

  1. Your outer loop's condition is for inequality:

     while target_months:= number_of_months:

There are two problems: If the values are not integers, it can happen, you have values that are very similar, but not quite equal, for example: 1.0000001 and 1.

Or, the number might skip that exact value:

x = 5
while x != 0:
    x = x - 2

In this segment, values of x will be: 5, 3, 1, -1, -3, -5, ... but never 0, so the look will go forever.

  1. You set the value number_of_months=0 inside the loop which tests its value (the outer loop)

  2. The value for last is arbitrarily set to 10000. Is it possible it's too small? Should it depend on the values of inputs?

Alright, I finally figured it out guys. annual_salary should be set back to its orginal user input value before the start of the second while loop. Otherwise anuual_salary will keep on increasing by the semi annual raise amount and therefore, lower and lower savings rate would be accepted as annual salary keeps rising.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM