值 'snake' 不支持成员测试 python 或 python 3.6

value 'snake' doesn't support membership test python or python 3.6

from random import randint
import turtle

wn = turtle.Screen()

if snake.xcor and snake.ycor == apple.xcor and apple.ycor:                             
    food = []
    Main_Score += 1
    pen.write("score:{}".format(Main_Score), align="center", font=("courier", 24, "normal"))
    while food == []:
        food = [randint(1, 18), randint(1, 58)]      
        if food in snake: food == []
    wn.addch(food[0], food[1], '*')

我已经查看了其他一些问题,行 if food in snake: food == [] 将不起作用,因为值 'snake' 不支持成员资格测试

如果你想比较位置那么你应该使用

food == snake.pos()

相似

snake.pos() == apple.pos()

(snake.xcor(), snake.ycor()) == (apple.xcor(), apple.ycor())

你的线路

if snake.xcor and snake.ycor == apple.xcor and apple.ycor:   

是错误的,因为您在 xcor()ycor() 中忘记了 (),因为它的意思是

if (snake.xcor) and (snake.ycor == apple.xcor) and (apple.ycor):   

所以 and 没有像您预期的那样工作。


而不是

while food == []:

你可以写

while not food: