值“ snake”不支持成员资格测试python或python 3.6

时间:2019-12-14 09:16:25

标签: python 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], '*')

我看了其他一些问题,由于值“ snake”不支持成员资格测试,因此if food in snake: food == []行不起作用

1 个答案:

答案 0 :(得分:0)

如果要比较位置,则应使用

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: