Why is this code giving error:time limit exceeded i but adding that extra code it works | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why is this code giving error:time limit exceeded i but adding that extra code it works

a=int(input()) b=1 while a>0: b=b*a a=a-1 print(b) Output:time limit exeeded a=int(input()) b=1 while a>0: b=b*a a=a-1 print(b) Output :wrong, is not factorial... a=int(input()) b=1 while a>0: b=b*a a=a-1 a=a-1 print(b) Output:its working So why it works the last time.. I coudnt underatand the logic.. Please somebody explain

1st Feb 2019, 5:15 PM
Sravan Das
Sravan Das - avatar
3 Answers
+ 2
You're welcome mate, but is it working now? especially the second or third? if you still unsure you are welcome to ask away ... 😁
1st Feb 2019, 6:11 PM
Ipang
+ 2
The first one triggers time limit because the `a = a - 1` wasn't indented, <a> was never decremented in the loop, because that line where <a> was decremented was out of while loop block. The second and third are the same, except for the extra `a = a - 1` line before the <b> value is printed. That extra line doesn't change <b> value at all, because it is out of loop block. I tested the second and third with input 5 and got good result (120), I am not sure what you mean by the second one is not giving the factorial of the input, can you tell what input and results you get from the second and third code?
1st Feb 2019, 5:48 PM
Ipang
+ 1
Hello thank you for your reply.... Ya i checked the code again and its working.. I dont know why it didn't gave me the answer... maybe some mistake in the way i entered when i first tested it..... And i m just new to it..!
1st Feb 2019, 6:04 PM
Sravan Das
Sravan Das - avatar