Reputation: 51
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
Upvotes: 0
Views: 70
Reputation: 356
You should define next as [1,4]
not [[1,4]]
to be able to test if next is in snake
Upvotes: 2