0

What is problem?

I want to play a card game There are some rules 1. There are 32 cards with 0 and 1 on each sides 2. Line up the cards with 0 side facing up 3. Put all the cards upside down in order, starting from left 4. Stop when the top side is changed from 0 to 1 (You did 1 time) 5. If you stop, leave the cards as they are and restart, following the rules 6. When the top side is changed from 1 to 0, continue(don't stop) In what order are cards lined up when you do this 32 times? (I'm super newbie on programming) list = [0] * 32 #There are 32 cards z = 1 while z <= 32: #Plays 32 times x = 0 if list[int(x)] == 0: #0 -> 1 list.remove(int(x)) list.insert(int(x), 1) x = x + 1 else: if list[x] == 1: #1 -> 0 list.remove(int(x)) #System says this line is problem list.insert(int(x), 0) z = z + 1 print(list)

16th Apr 2020, 8:41 AM
김승원
김승원 - avatar
1 Réponse
+ 1
The remove() method removes the 1st matching element: lst = [1,2,3,2,4] lst.remove(2) print(lst) # output: [1,3,2,4] if you want to remove the element at the index x, you need to do in another way ;) ... but do you really want to do it by this way? As your purpose is to invert the value at index x, you could simply do in one instruction: lst = [1,2,3,2,4] lst[3] = 0 print(lst) # output: [1,2,3,4] Also, your logic seems bad: I would say (not tested) that inside your while loop, in the 'True' conndition of you if else (so, in the 1st if block), you should iterate over the list (and invert the value) while the new value is not 1... at least ;)
16th Apr 2020, 3:13 PM
visph
visph - avatar