![]() |
Need help with a while loop - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Need help with a while loop (/thread-42230.html) |
Need help with a while loop - ampereap - May-31-2024 I'm teaching myself Python over the summer, and I am using the MIT OWC class to get started. In the second problem set we are trying to figure out how many months it will take to save enough money to afford a down payment on a house. I created a loop to count how many times it's running with the total count = the number of months it will take to save the down payment. But the loop is infinite and I can't figure out why. Here is my code: #set first month's savings to the amount you're saving per month #this is 1000 per month based annual salary given in the problem set current_savings = monthly_savings #iterate and count the number of iterations while current_savings < portion_down_payment: current_savings = (current_savings * .0033) + (monthly_savings) num_of_months += 1 total_months = num_of_months+1Some clarification: portion_down_payment is 30k based on the problem set, and an extra wrinkle is that we need to also add a 4% annual investment based on the current total savings. Basically after each month we invest the entire savings and get .0033 multiplier, and add that to the new month's savings (1000). What I think I'm coding: the first month just equals what we save per month. current_savings = monthly_savings no investment. then we need to iterate the rest of the months dropping into the while loop while current savings is less than down payment execute the code block in the code block set current_savings = our current value of current_savings times .0033(investment) and add our new monthly saved amount increment num_of_months then since we were true it should run again and it does, but my current_savings is not going up. it runs correctly the first iteration and then the variable doesn't move. So I know i'm not expressing that part of the code correclty. I just can't figure out why it's wrong. Been working on it for two days. I know this is long hope you guys can help. RE: Need help with a while loop - deanhystad - May-31-2024 You throw away the balance each time through the loop. Multiply by 1.0033. RE: Need help with a while loop - Pedroski55 - May-31-2024 Just for fun: #annual_interest = 0.04 = 0.04 / 12 = 0.0033333333 ... per month portion_down_payment = 30000.00 # get how much you saved + interest of 0.0033 on savings each month def savings(cs, ms): accrued = cs * 1.0033 + ms yield accrued #annual_interest = 0.04 = 0.04 / 12 = 0.0033333333 ... per month portion_down_payment = 30000.00 monthly_savings = 1000 current_savings = 0 months = 0 while current_savings < portion_down_payment: months +=1 current_savings = next(savings(current_savings, 1000)) if months % 12 == 0: print(f'\nBeen saving for {months / 12} year(s) now, when can I move in???\n') print(f'months = {months}, current_savings = {round(current_savings, 2)}.') if (portion_down_payment - current_savings) < 1000: print(f'\nYou only need to pay {round(portion_down_payment - current_savings, 2)} this month and you are done!') print("Please borrow a lot of money from us, so we can charge you much more than 4% annual interest!") current_savings = portion_down_payment RE: Need help with a while loop - ampereap - May-31-2024 (May-31-2024, 02:36 AM)deanhystad Wrote: You throw away the balance each time through the loop. Multiply by 1.0033. lol thank you. I didn't even think the math was the problem, but of course... I appreciate your help! RE: Need help with a while loop - ampereap - May-31-2024 (May-31-2024, 08:08 AM)Pedroski55 Wrote: Just for fun: Thanks, I am brand new to Python and am not familiar with yield or next yet. I will dive into this though and add it to my knowledge. I appreciate you taking the time to help. |