Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help with a while loop
#1
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+1
Some 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.
Larz60+ write May-31-2024, 11:40 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Tags added for you. Please use BBCode tags on future posts.
Reply
#2
You throw away the balance each time through the loop. Multiply by 1.0033.
Reply
#3
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
Reply
#4
(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!
Reply
#5
(May-31-2024, 08:08 AM)Pedroski55 Wrote: 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

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.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020