Question:
I am attempting to script a short code to figure out the number of days it takes to reach a given principal in the bank due to daily interest. Using my code below does not yield any errors when run in IDLE, but the counter returns 0. Any ideas what I missed?
def main(): # irrelevant code elided by msw, Bal, Int and Tar are numeric counter = 0 for i in range(0): if (Bal * Int) == Tar: print '1' else: counter + 1 print counter
Solution:1
I'm not sure what you're getting at with this loop:
for i in range(0): if (Bal * Int) == Tar: print '1' else: counter + 1

range(0)
is an empty list, so the loop won't execute at all. 
counter + 1
simply calculates one more than counter, it won't increment counter, you probably meancounter += 1
 There's nothing in the loop that changes at each iteration, so if you ever get into it, it will be an infinite loop.
Solution:2
I believe the formula to calculate final balance with interest is:
Final = Principal * ( 1 + interest ) ** interest_period
Assuming I got this correct, then you can find out how many interest periods it will take by:
def how_long(start_money, interest_rate, final_money): day = 0 money = start_money while True: if money >= final_money: break day += 1 money = start_money * (1 + interest_rate)**day return day, money
Solution:3
In [5]: def test(): ...: for i in range(0): ...: return '1' ...: ...: In [6]: x = test() In [7]: print x > print(x) None
See the return value is 'None'. I don't know what are you trying to do. But The basic mistake is the Argument of range(x) function. The range(0) always return empty list.
Solution:4
That's because you put range(0) which is an empty loop. Perhaps you could consider a while loop?
Solution:5
Your loop for i in range(0)
doesn't actually execute. range(0)
returns an empty list []
which will skip the body of your for loop.
Solution:6
Please explain what you think this does? Please update your question with an Englishlanguage explanation of how many times you think this look will work.
counter = 0 for i in range(0): if (Bal * Int) == Tar: print '1' else: counter + 1
Hint. The answer is zero. The question is "why?" and "what were you trying to do?"
Solution:7
You have been told the three or more problems with your code. If there's no particular reason to use a loop, it's better calculated with a formula:
future_value = present_value * (1 + interest_rate_per_period) ** number_of periods
or, for short,
f = p * (1 + i) ** n f / p = (1 + i) ** n log(f / p) = n * log(1 + i) n = log(f / p) / log(i + i)
Example: I have $5000; how many years will it take to grow to $10000 at 10% per annum?
>>> from math import log >>> f = 10000.0 >>> p = 5000.0 >>> i = 0.1 >>> n = log(f / p) / log(1 + i) >>> n 7.272540897341713 >>>
Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
EmoticonEmoticon