+ 12
What will be the output of this code?
t = [2,3,4] for i in t: if (i % 2): t.append(i) print(t)
39 Answers
+ 11
Is the code is correct
+ 5
Bensen this line of code is correct.
i%2 will always give either 1 or 0, which is considered as True or False.
+ 4
šš¢š¢ššØ šš”šš²šš„, Abhay I am new at this platform. This is the code in which I was facing problem and had a doubt which has already been clarified by ~ swim ~ . Thanks for telling me about Code Playground. But there also, this code is giving 'No Output' and no explaination.
+ 4
Ok wlc to sololearn but you asked what is the output of the code !!You could have said this program is giving no output !!
Also click on your profile there is code bits ,click on add sign and you can access the code playground for the given languages !
+ 3
šš¢š¢ššØ šš”šš²šš„ Thankyou for understanding. I got the logic behind this code. š
+ 3
I accept it! Will improve it from next time.
+ 3
šš¢š¢ššØ šš”šš²šš„ execute this program once, it is a part of infinite loop. Similar to below code:
if True:
print("true")
+ 2
+ 2
šš¢š¢ššØ šš”šš²šš„ this is one of the question which I've answered at this platform only and that too in Python challenge.
+ 2
Riddhi Rana
If I correct this code as
t = [2,3,4]
for i in t:
If( i % 2):
t.append(i)
print(t)
Then at 3 if statement became true so 3 will append in t
So again I became 3 then also it append and I form a infinite loop [2,3,4,3,3,3,3,3,3,.............]
+ 2
Bensen, šš¢š¢ššØ šš”šš²šš„ you are ryt...my mistake...i've written this code in description multiple times and the text was getting disappeared everytime...I missed writing 'if' but ~ swim ~ has given the logic I was looking for, it is satisfactory answer.
Now when I saw Bensen's code, I compared it with mine...then I found it...In my mind it was if a%2: and the code I've written here is a%2:......my bad šš
+ 2
There will be an infinite loop
Because at every i%2 it is gonna add a value i again and again
+ 1
There is code playground ,use it pls
+ 1
Riddhi Rana
It cause error
the statement
i%2:
is wrong it give a syntax error message
+ 1
I have more questions
+ 1
shubhra sharma ask me the question
+ 1
Pavithra Kanmanirajah
No
This code is incorrect at
i%2:
It may be
if(i%2):
+ 1
It's ok these are the usual mistakes in coding ššš
+ 1
Behruz Rajabov you can ask questions here