I have the code:
turn = [] while 1 > 0: turn.insert(0, 2) print(turn) at the output I want to get [2], but he adds each time to a new place 2, like this: [2, 2, 2, 2, 2, ... 2]. What am I doing wrong?
I have the code:
turn = [] while 1 > 0: turn.insert(0, 2) print(turn) at the output I want to get [2], but he adds each time to a new place 2, like this: [2, 2, 2, 2, 2, ... 2]. What am I doing wrong?
Try this:
turn = [] turn.insert(0, 2) print(turn) First of all, 1 > 0 always True , so it’s more beautiful to write
while True: But the result will be an endless loop , and so you need to stop it somewhere with the break command, which you do not do.
Now to what you wanted to achieve. Instead of your complex code just write
turn = [2] print(turn) Probably because you use an infinite loop, in the body of which you insert a two at the beginning of the array
list.insert (i, x) Inserts on the i-th element the value of x You are using an infinite loop. Easier, really, to write:
turn[0]=2 if you need to assign this value to the zero index.
Source: https://ru.stackoverflow.com/questions/881889/
All Articles