0

How do I get my code to print out one number for how many times my while loop ran?

I'm writing a code to determine how many months it would take for me to pay for a house. I'm trying to get the last section of code to give me a single number for how many times my loop ran, instead of printing every single iteration(which inevitably always ends with a time out error). total_cost=float(input("Enter the cost of your dream home: ")) print(total_cost) annual_salary=float(input("Enter your annual salary: ")) print(annual_salary) monthly_salary=annual_salary/12 portion_saved=float(input("Enter the percentage of your salary to save, as a decimal: ")) print(portion_saved) portion_down_payment=0.25*total_cost r=0.04 current_savings=portion_saved*r/12 mortgage=total_cost-portion_down_payment total_cost=mortgage while total_cost>current_savings: print("Number of months: ", current_savings) current_savings+=1

11th May 2019, 6:25 PM
Vulpix
Vulpix - avatar
33 Answers
+ 5
hi, besides all python issues I have a question what this program is calculating. I give input: 100000 price for house 50000 yearly salary 20 or 0.2 or 15 % to save output is always 75000 months. ??? what does this mean ???
11th May 2019, 8:50 PM
Lothar
Lothar - avatar
+ 4
hi Laura, no problem - we will find the bug. It would be very helpful if you could do a calculation without this code. Please take 3 required values and then post it together with your result.
12th May 2019, 7:05 AM
Lothar
Lothar - avatar
+ 4
To be honest - I still have doubts about about the way the program is calculating. Laura - you are the only one who can give us a target. I am sure you already have done some calculations in the past, so it would be helpful to see them. All input valuues, description and approved results. Thanks in advance.
12th May 2019, 4:28 PM
Lothar
Lothar - avatar
+ 3
hi, i have made a trial with numpy which has a ready-to-go function to calculate the number of monthly payments. Try it here: https://code.sololearn.com/cSCzR1tF7Zib/?ref=app
12th May 2019, 11:34 AM
Lothar
Lothar - avatar
+ 3
Laura, i just tried your code with the values you send me and i get the result: Enter the cost of your dream home: 100000 100000.0 Enter your annual salary: 70000 70000.0 Enter the percentage of your salary to save, as a decimal: 0.5 0.5 Number of months: 4 I am afraid there is still an issue in the way the program is calculating.
13th May 2019, 8:25 PM
Lothar
Lothar - avatar
+ 3
Donā€™t worry. If we know the correct amount of total savings/ curent savings, itā€˜s only some small changes in code so than we are done.
14th May 2019, 4:58 PM
Lothar
Lothar - avatar
+ 3
Laura, i will publish 2 versions of code for you, but this will be tomorrow.
16th May 2019, 3:31 PM
Lothar
Lothar - avatar
+ 3
Hi Laura, congratulations! You are all set now! If you like you can remove the line: #current_savings*=months it is already marked as comment with #, so itā€™s already not used during execution run of the code. Allow me just a remark in writing code: inv=r*monthly_salary this code is difficult to read because there are used no whitespaces. inv = r * monthly_salary this code has a better readibility. It does not make any difference in code execution. You can leave your current code as it is. All the best!
17th May 2019, 9:08 AM
Lothar
Lothar - avatar
+ 3
you are welcome!
17th May 2019, 3:41 PM
Lothar
Lothar - avatar
+ 2
while_count = 0 while ...: while_count += 1 print(while_count) This is the simplest way of doing this.
11th May 2019, 6:49 PM
Russ
Russ - avatar
11th May 2019, 8:01 PM
Russ
Russ - avatar
+ 2
Laura, did you run this code, and if yes: what arguments did you input for cost of the house, yearly salary and % of savings? What is the result?
13th May 2019, 8:15 PM
Lothar
Lothar - avatar
+ 2
hi - good news! Now we are about to reach the goal. we have to change only in code: change like this: months=0 #add this new line: temp = current_savings while total_cost>current_savings: #current_savings*=months current_savings += temp months+=1 print("Number of months: ", months) So with the arguments: total cost: 100,000 yearly salary: 75,000 % savings fron salary: 0.5 We get a result of 22 months. Anyway there is still a question from my side but i will come back later.
14th May 2019, 9:59 AM
Lothar
Lothar - avatar
+ 2
Laura, Russ itā€˜s me again. My question is: In Lauras description / breakdown is ā€œtotal savingsā€ mentioned with a value of 3,033.34. This term does not appear in the code. There is an other term in code ā€œcurrent_savingsā€ with a value of 3375 at the moment the program runs in the loop the first time. Laura can you check this what this difference means?
14th May 2019, 10:40 AM
Lothar
Lothar - avatar
+ 2
Congratulations Laura! You have created your first full code. Let's hope it is the first of many. Not a problem, it was my pleasure. If you are interested, I updated the code I shared much earlier in the thread. I generalised it a touch by asking what interest rate they can get and I implemented a slightly different algorithm where all savings earned interest. Good luck and keep going!
18th May 2019, 8:04 AM
Russ
Russ - avatar
+ 1
Your print statement is still in your while loop. All lines that are indented are in your loop. De-indent the line with your print statement to take it out of the while loop (as I showed in my previous comment).
11th May 2019, 7:56 PM
Russ
Russ - avatar
+ 1
Yes. It looks like that fixed it. Thank you!
11th May 2019, 8:03 PM
Vulpix
Vulpix - avatar
+ 1
If you would like a friendly nudge, every time your while loop loops, your current_savings variable is only incremented by 1, rather than the amount of money that would be saved each month (monthly_salary*portion_saved). This means that, whatever your monthly salary is and however much you can afford to put away each month, only $/Ā£1 is saved. This is why a similar output is consistently seen as it stands at the moment.
12th May 2019, 7:51 AM
Russ
Russ - avatar
+ 1
That would be a good start I think. Feel free to test it out at your convenience. What may help us to help you, is if you try and describe what algorithm you are trying to implement and then we can direct you more effectively. I'm more than happy to give advice and nudges in the right direction, but I don't want to do it all for you as that doesn't help you learn.
12th May 2019, 2:54 PM
Russ
Russ - avatar
+ 1
Your final print statement doesn't print the number of months, it prints the current_savings variable. That is the variable upon which the while loop hangs (if the current_savings is less than the total money required, it loops again). So if your dream house is 100,000, your code loops until current_savings reaches 75,000, and then prints the current_savings variable. This is why you keep getting the same output. I think this is what you need to address before anything else. Try introducing a new 'month' variable and use that as a loop counter. Then your final print statement could be print("Number of months:", month) If you can do this, you should start to see new outputs that can help you work through what else needs doing. Ideally, if you are able to share your current code to this thread, we will be able to see where you are at through the rest of the time you need help from us.
13th May 2019, 5:37 PM
Russ
Russ - avatar