Please help with the code - 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: Please help with the code (/thread-2034.html) |
Please help with the code - zarnav - Feb-12-2017 Hi guys, my code should do this: I have an account of 10000$ and every month I take interest of 0.5% and also I substract 500$ every month I need for my program to stop when the account reaches 0. And also all the years until depletion to be listed. PS: I am a total beginner:) Please se my code below which is not displaying an output.Please advise. account = 10000 month = account/0.5-500 year = 12*month for i in range(account): if account == account/0.5-500: if account == 0: break else: print(year) RE: Please help with the code - wavic - Feb-12-2017 The interest should be account*0.5 This looks like homework to me RE: Please help with the code - zarnav - Feb-12-2017 Hi Wavic thank you for replying. I am studying for a course and I am trying to resolve all the problems from each chapter from Python for everyone - Cay S. Horstmann and I am stuck with this code. It is not literally my homework but I think is a problem from the book. RE: Please help with the code - wavic - Feb-12-2017 Your loop is completely wrong. You iterate over account balance which is $10000 so you will do 10000 loops eventually. Instead of years. You check for == to 0 but probability the balans got equal to 0 is minimal. This will fail. Check for <= 0. RE: Please help with the code - zarnav - Feb-12-2017 Ok so I have changed my code a bit but is still printing now -139 months which is wrong. initial_balance = 1000 rate = 10 withdraw = 500 result = 1 target = result balance = initial_balance month = 1 while balance>result: interest = balance*rate/100 balance = balance - interest - withdraw month = month-interest if balance == month: print (month) print ("no of months is", month) RE: Please help with the code - ichabod801 - Feb-12-2017 You're subtracting the interest. Aren't you supposed to add the interest? Also, you don't want to subtract the interest from the month, you want to increase the month by one each time through the loop. You can do that with month += 1 .
RE: Please help with the code - sparkz_alot - Feb-13-2017 You should use more descriptive variable names. Going back to your original parameters, you are going to need five variables:
principle = 10000 interest = .5 / 100 withdraw = 500 month = 0.0 years = 0.0In your conditional loop, you want to repeat a process until the condition is no longer True, in this case, keep repeating until there is not enough money left to cover the withdraw and interest. Here we add a sixth variable we''l call 'new_interest'. There are two ways to calculate the principle, subtract the interest then make the withdraw (bank makes money) or make the withdraw then subtract the interest (bank makes less money). Your conditional should look something like this now: while principle >= withdraw: new_interest = interest * principle principle = principle - new_interest - withdraw month += 1and finally: print("no of years is", round(month / 12, 3))You could add two additional variables: total_interest and total_withdrawal and test your results: print('Test = ', round(principle + total_interest + total_withdrawal, 2))You should come very close, if not spot on to your original principle (may be slightly off due to rounding) RE: Please help with the code - wavic - Feb-13-2017 Hm, is that value (0.5) a percentage per month? If it is you should use it in the script but you have to divide it by 100. Now you get $500 per month interests. account = account + account*(0.5/100) - 500 This add the interest to the balance and subtracts 500. |