Why the output is 9 ...... | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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))

9th Jul 2020, 7:07 PM
Akash Gupta
Akash Gupta - avatar
46 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
9th Jul 2020, 7:55 PM
A͢J
A͢J - avatar
+ 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.
9th Jul 2020, 7:26 PM
Russ
Russ - avatar
+ 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.
10th Jul 2020, 11:01 PM
BlackRose Mike
BlackRose Mike - avatar
+ 6
Show me your Code.
9th Jul 2020, 7:59 PM
A͢J
A͢J - avatar
+ 6
Akash Gupta In your post and in this question condition is different. Why?
9th Jul 2020, 8:01 PM
A͢J
A͢J - avatar
+ 6
Akash Gupta I already solved with explaination.
9th Jul 2020, 8:09 PM
A͢J
A͢J - avatar
+ 6
AJ Anant - C#/Java Challenger Got it .....✌️✌️
9th Jul 2020, 8:12 PM
Akash Gupta
Akash Gupta - avatar
+ 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.
9th Jul 2020, 7:16 PM
Abhay
Abhay - avatar
+ 5
Akash Gupta According to your code you are printing the length inside loop that's why you are getting output like that.
9th Jul 2020, 8:11 PM
A͢J
A͢J - avatar
9th Jul 2020, 8:11 PM
Akash Gupta
Akash Gupta - avatar
+ 5
Ishu Kumar There is no difference. No need to update question. In description he also mentioned why the output 9.
11th Jul 2020, 5:38 AM
A͢J
A͢J - avatar
+ 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.
9th Jul 2020, 7:35 PM
Russ
Russ - avatar
+ 4
𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 We each had a different guess and both were wrong! 😆
9th Jul 2020, 7:46 PM
Russ
Russ - avatar
+ 4
Akash Gupta My explanation above is still correct...
9th Jul 2020, 7:47 PM
Russ
Russ - avatar
+ 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 .
11th Jul 2020, 11:16 AM
Shubham Singh
Shubham Singh - avatar
9th Jul 2020, 7:32 PM
Russ
Russ - avatar
+ 3
𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 this question is in the challenge ....and opponent mark the option which is 9
9th Jul 2020, 7:33 PM
Akash Gupta
Akash Gupta - avatar
+ 3
𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 i check lot of time .how can i show you screenshot of that
9th Jul 2020, 7:36 PM
Akash Gupta
Akash Gupta - avatar
+ 3
Akash Gupta Post the picture on your own feed, then copy the link for your post into this thread
9th Jul 2020, 7:39 PM
Russ
Russ - avatar
+ 3
𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 sorry bro my mistake and sry for wasting your time ... please explain it
9th Jul 2020, 7:46 PM
Akash Gupta
Akash Gupta - avatar