+ 1
Why does my code return "time limit exceeded"
Write a function, investment(PMT, n, i) that calculates a customer's savings at some point in the future, if: ⢠an amount is invested at the End of every year, starting with amount of PMT at the end of this year, ⢠at an interest rate of i% per year, compounded annually, ⢠the investment doubles every second year(cumulatively). ###START FUNCTION def investment(PMT, n, i): # YOUR CODE HERE return round(investment_balance, 2) ###END FUNCTION. investment (15000, 30, 0.1045) == 1954935238.47 https://code.sololearn.com/c3zpUsmMrm2c/?ref=app https://code.sololearn.com/c3zpUsmMrm2c/?ref=app
7 Answers
+ 3
I have given it a try, the code works but the calculation is somewhat wrong:
def investment(PMT, n, i):
x = 0
investment_balance = PMT
while x < n:
if x % 2 == 1:
investment_balance *= 2
investment_balance *= (1+i/100)
investment_balance += PMT
x += 1
return float(round(investment_balance, 2))
print(investment(15000, 30, 0.1045))
+ 3
lYou increase x only if x % 2 = 1.
It will never happen, your code loops forever.
+ 2
Not forgetting to thank Paul Jacobs for breaking it down for me and correcting me where i was wrong. You the best Paulđ
+ 1
What would you suggest i do to get an output 1954935238.47 given the arguments
+ 1
The code you just provided run fine yes, i just wonder why does it output such a low value though.
+ 1
Thanks Ina it returned a more reasonable value, abit more than it should but close enough to be accepted. Thank you so muchđđ