第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

當(dāng)一只正在移動(dòng)的 Python 烏龜靠近另一只烏龜時(shí)停止它

當(dāng)一只正在移動(dòng)的 Python 烏龜靠近另一只烏龜時(shí)停止它

胡子哥哥 2021-08-14 17:12:39
while當(dāng)另一只烏龜有 50 個(gè)單位時(shí),如何使用循環(huán)停止隨機(jī)移動(dòng)的烏龜?我有一只烏龜隨機(jī)選擇一個(gè)位置并創(chuàng)建一個(gè)大點(diǎn)或洞,另一只烏龜隨機(jī)移動(dòng) 90 度轉(zhuǎn)彎并每次向前移動(dòng) 50 個(gè)單位。隨機(jī)移動(dòng)的烏龜在離開屏幕末端時(shí)停止,但是當(dāng)烏龜?shù)竭_(dá)另一只烏龜創(chuàng)建的洞時(shí),我如何也讓烏龜停止?import randomimport turtledef turtlesClose(t1, t2):    if t1.distance(t2)<50:        return True    else:        return Falsedef isInScreen(win,turt):    leftBound = -win.window_width() / 2    rightBound = win.window_width() / 2    topBound = win.window_height() / 2    bottomBound = -win.window_height() / 2    turtleX = turt.xcor()    turtleY = turt.ycor()    stillIn = True    if turtleX > rightBound or turtleX < leftBound:        stillIn = False    if turtleY > topBound or turtleY < bottomBound:        stillIn = False    return stillIndef main():    wn = turtle.Screen()    # Define your turtles here    june = turtle.Turtle()    july = turtle.Turtle()    july.shape('turtle')    july.up()    july.goto(random.randrange(-250, 250, 1), random.randrange(-250, 250, 1))    july.down()    july.dot(100)    june.shape('turtle')    while isInScreen(wn,june):        coin = random.randrange(0, 2)        dist = turtlesClose(july, june)        if coin == 0:            june.left(90)        else:            june.right(90)        june.forward(50)        if dist == 'True':            breakmain()
查看完整描述

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()


查看完整回答
反對(duì) 回復(fù) 2021-08-14
  • 1 回答
  • 0 關(guān)注
  • 249 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)