+ 9
Why the output is 9 ......
#this code giving me indentation error on print statement please solve this and tell me why the output is 9 a=[1,2,3,4,12,13] for i in a: if i%2==1: a.append(0) Print (len(a))
56 Answers
+ 36
Akash Gupta
length is 9 because we are appending in original array.
Explaination:
a = [1, 2, 3, 4, 12, 13]
According to condition
1 % 2 == 1 //true
Now a will be a [1, 2, 3, 4, 12, 13, 0]
3 % 2 == 1 //true
Now a will be a [1, 2, 3, 4, 12, 13, 0, 0]
13 % 2 == 1 //true
Now a will be a [1, 2, 3, 4, 12, 13, 0, 0, 0]
Finally length will be 9
see here..
https://code.sololearn.com/cGydxwaIInsV/?ref=app
+ 8
To add to Abhay 's answer: for the output to just be 9, the if should be if i%2 != 0, and print should be de-indented.
Length of a starts out at 6 and, for every odd number (assuming the 'if' is amended as above), a 0 is added to it. As this happens 3 times, len(a) is 9 at the end.
+ 7
I believe the correct answer is AJ Anant .
If its remainder 1(odd number), append 0 to the original list (not the number).
From the list 3 of the numbers are odd so three zeros are added to the original list.
Length of list would be 9 not infinity.
Would only be infinity if the original numbers which are odd are added back.
+ 6
Show me your Code.
+ 6
Akash Gupta In your post and in this question condition is different. Why?
+ 6
Akash Gupta I already solved with explaination.
+ 6
AJ Anant - C#/Java Challenger Got it .....✌️✌️
+ 5
It doesn't gives any indentation errors ,perhaps the error maybe about print since it is lowercase not uppercase
Also the output is
6
7
7
8
9
9
10
11
12
.
.
infinite
Which is right.
+ 5
Akash Gupta According to your code you are printing the length inside loop that's why you are getting output like that.
+ 5
𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 andAJ Anant - C#/Java Challenger thanks for the help ......
+ 5
Ishu Kumar There is no difference. No need to update question. In description he also mentioned why the output 9.
+ 4
Akash Gupta Please double check the code in the challenge. This code will not give 9. If this is what the code says, report question for having wrong answer.
+ 4
𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 We each had a different guess and both were wrong! 😆
+ 4
Akash Gupta My explanation above is still correct...
+ 4
Be cause you are printing length of the list and also appending 0 when the elements of the list give a remainder of 1 when divided by 2 . And there are three cases when the remainder is 1 when the elements of the list is divided by 2 i.e. 1, 3, 13 .
That's why 0 has been appended 3 times in ur list and ur resultant list looks like
[1,2,3,4,12,13,0,0,0]
You can easily count that there are 9 elements in the list .
That's why the result is 9 .
Hoping that u will understand .
If the issue remains unsolved kindly message me .
+ 3
𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 Yeah, either way works!
+ 3
𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 this question is in the challenge ....and opponent mark the option which is 9
+ 3
𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 i check lot of time .how can i show you screenshot of that
+ 3
Akash Gupta Post the picture on your own feed, then copy the link for your post into this thread
+ 3
𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 sorry bro my mistake and sry for wasting your time ... please explain it