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:
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.
1 2 3 4 5 6 7 8 9 10 |
#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 |
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.
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.