1 回答

TA貢獻(xiàn)1865條經(jīng)驗(yàn) 獲得超7個(gè)贊
您的代碼的問題是以下語句:
if dist == 'True':
你不想要引號(hào)周圍True。雖然這會(huì)起作用:
if dist == True:
正確的表達(dá)方式是:
if dist is True:
或者更好:
if dist:
否則你的代碼似乎工作。下面是利用一些海龜習(xí)語和其他代碼清理的重寫:
from random import randrange, choice
from turtle import Screen, Turtle
CURSOR_SIZE = 20
def turtlesClose(t1, t2):
return t1.distance(t2) < 50
def isInScreen(window, turtle):
leftBound = -window.window_width() / 2
rightBound = window.window_width() / 2
topBound = window.window_height() / 2
bottomBound = -window.window_height() / 2
turtleX, turtleY = turtle.position()
return leftBound < turtleX < rightBound and bottomBound < turtleY < topBound
def main():
screen = Screen()
july = Turtle('circle')
july.shapesize(100 / CURSOR_SIZE)
july.up()
july.goto(randrange(-250, 250), randrange(-250, 250))
july.down()
june = Turtle('turtle')
while isInScreen(screen, june):
if turtlesClose(july, june):
break
turn = choice([june.left, june.right])
turn(90)
june.forward(50)
main()
添加回答
舉報(bào)