Why does my code return "time limit exceeded" | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

30th Sep 2018, 3:52 PM
Mantrys
Mantrys - avatar
6 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))
30th Sep 2018, 5:14 PM
Paul
Paul - avatar
+ 3
lYou increase x only if x % 2 = 1. It will never happen, your code loops forever.
30th Sep 2018, 4:23 PM
Paul
Paul - avatar
+ 2
Not forgetting to thank Paul Jacobs for breaking it down for me and correcting me where i was wrong. You the best Paul🎖
30th Sep 2018, 7:24 PM
Mantrys
Mantrys - avatar
+ 1
What would you suggest i do to get an output 1954935238.47 given the arguments
30th Sep 2018, 4:37 PM
Mantrys
Mantrys - avatar
+ 1
The code you just provided run fine yes, i just wonder why does it output such a low value though.
30th Sep 2018, 5:41 PM
Mantrys
Mantrys - avatar
+ 1
Thanks Ina it returned a more reasonable value, abit more than it should but close enough to be accepted. Thank you so much🏆😊
30th Sep 2018, 7:22 PM
Mantrys
Mantrys - avatar