简体   繁体   中英

Finding an item in a two-dimensional list in Python

I'm trying to program the game 'Snake' in Python, but i can't find out if an item like next, containing x and y variables, is inside the two-dimensional list snake. I wrote this code to demonstrate my problem:

from sense_hat import SenseHat
sense = SenseHat()
import time

#Variables
dead = False
##List for snake consiting of 4 white pixels
snake = [[1, 4], [2, 4], [3, 4], [4, 4]] 
last = snake[-1]
next = list(last)   

###FUNCTIONS

def check():
  if next in snake:
    dead = True


##MAIN      
sense.clear()
check()

while dead == True:
    sense.show_message("dead")


while dead == False:
    sense.show_message("alive")

The output is always "alive", although next is in snake. I'm using the Sense HAT module emulator on: https://trinket.io/sense-hat

You should define next as [1,4] not [[1,4]] to be able to test if next is in snake

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM