0

Quiz for "for loop",see the code below

b=range(5) for i in b: print b b.remove(i) I get [0,1,2,3,4][1,2,3,4]and[1,3,4]! what's happening here? supposed to be [2,3,4]? b ever changed? version Python 2.7 And in Python3.x you could just change the statement: b=list(range(5)) and print()😀 another question: for i in b: if i want b excluded from a number let's say 3, any simple way to do that? like for i in b and i !=3 :

21st Oct 2017, 8:07 AM
lwluowei
8 Answers
+ 1
can you explain it? because in my head is smth like this: for 0 in [0,1,2,3,4] print [0,1,2,3,4] remove [0] for 1 in [1,2,3,4] print [1,2,3,4] remove [1] for 2 in [2,3,4] print [2,3,4] remove [2] for 3 in [3,4] print [3,4] remove [3] for 4 in [4] print [4] remove [4] from what i was reading, remove calls a variable value(first it that you can get from left to right) not the index of it :) if it was the index then the above solution will be: change b.remove(i) with del b[i] for 0 in [0,1,2,3,4] print [0,1,2,3,4] remove [index0=0] for 1 in [1,2,3,4] print [1,2,3,4] remove [index1=2] for 2 in [1,3,4] print [1,3,4] remove [index2=4] for 3 in [1,3] print [1,3] remove [index3=error probably] Even with my ideas right(i quess) i cant do what i want, so i belive I should install an IDE on my pc to compile the code :) so your quiz was somewhere or you wrote it? :)
23rd Oct 2017, 1:35 PM
derXred
derXred - avatar
0
i got this. i changed in b as index of b becoming larger.i =b[0],first round. i=b[1],2nd round. since b has changed.so this is the result.😀
23rd Oct 2017, 10:22 AM
lwluowei
0
for the second question: for i in b: if i!=3: print ("what you want to do")
23rd Oct 2017, 1:37 PM
derXred
derXred - avatar
0
consider i refer to b[j]secretly .this is the way computer does through the index one by one while walking the value of i.first round i=b[0],then i=b[1],just not the human way of thinking😀. my best guess.
23rd Oct 2017, 1:49 PM
lwluowei
0
well you say that my index code is corect :))) but your quiz and expectations for output are different things(or am i wrong cuz i cant compile well in SL?), so you got a code that does something and you post a different output?
23rd Oct 2017, 1:54 PM
derXred
derXred - avatar
0
Python 3 didn't work with the code,Python2.7 does,and i=0,2,4 corresponse the index 0,1,2 of changing b,the final result is range(5),range(1,5),[1,3,4],i am confused at first,i thought it supposed to be[2,3,4],you already got it.but i don't get your question well.
23rd Oct 2017, 2:16 PM
lwluowei
0
That was my confusion too,now make sense, so next time i have to see exactly what that "thing" does in each version of python....that is confusing :)) i will stick to c++ But to make it clear calling remove in python3 goes by the value and in python 2.7 goes by the index? can you post the code that is good(if you got it) and mention the python version? Maybe someone is looking in future for the same problem :)
23rd Oct 2017, 2:28 PM
derXred
derXred - avatar
0
nope,always remove the element,but Python3,will raise error:range object has no attribute remove,Python 2.7 work just fine.Since so many conversations goes around,i guess everyone will make it clear.have fun in learning Python😀
23rd Oct 2017, 2:41 PM
lwluowei