0
Why this output ????
11 Respostas
+ 5
Because the list elements are not numbers , so when you add "5"+" 2"+"3" you get "523" .
+ 4
btw: You can simplify your while loop.
while x:
y += x.pop()
The method pop removes the last element from the list and returns it - so exactly what you want here.
+ 1
You are relatively far in your Python tutorial, so you must have read it.
You should reread that chapter and experiment to get a feeling how it works.
Simply spoken it works like if: with a condition, for example x<5.
If x<5:
do this or that.
And while just means that as long as x < 5, the loop runs again and again and again, even forever - until (by the code inside the while block) x changes and is not <5 anymore.
Im your example while x means:
while there is anything at all in the list x...
+ 1
honfu thanks a lot for clearing my doubt
+ 1
My pleasure. :)
0
why new x is an empty list???
0
You always restore a one element smaller list in your x, and you do that 'while x'.
So in the last round you store everything except the last element as before - but given a one element list [3] that is... nothing.
[3] - last element == []
0
I am not getting how while is working
please some one explain me
0
So you haven't written the code yourself?
0
it was given in our book excercise
0
Anyway, loops are really important (like 90% of what's in the tutorial), so be sure to review all of that and write your own practice codes!