Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python语句while.distance_Python_Distance_Turtle Graphics - Fatal编程技术网

Python语句while.distance

Python语句while.distance,python,distance,turtle-graphics,Python,Distance,Turtle Graphics,据我所知,以下代码 turtle.forward(50) 意味着在屏幕上移动海龟 以下是我试图学习的代码: def forward(distance): while distance > 0: if turtle.distance(0,0) > 100: angle = turtle.towards(0,0) turtle.setheading(angle) turtle.forward(1) distance = dista

据我所知,以下代码

turtle.forward(50)
意味着在屏幕上移动海龟

以下是我试图学习的代码:

def forward(distance):
while distance > 0:
    if turtle.distance(0,0) > 100:
        angle = turtle.towards(0,0)
        turtle.setheading(angle)
    turtle.forward(1)
    distance = distance - 1
我真的不明白怎么做

turtle.forward(1)
distance = distance -1
在这里工作。我知道如果距离大于100,那么海龟会转身从末端位置移动到小于100的位置。我尝试过它,但我仍然不清楚代码turtle.forward(1)是什么意思。海龟应该移动1像素吗?最后的距离比另一个像素小?当我输入不同的数字而不是1时,我会得到奇怪的结果。很抱歉问这个问题-我学习很慢。 谢谢

海龟前进(1)
告诉海龟在当前方向前进一个像素

如果是海龟。距离(0,0)>100
检查海龟离原点(窗格中心)的距离是否大于100

角度=海龟。朝向(0,0)海龟。设置航向(角度)
将海龟转向原点

下面是它如何结合在一起的:

def forward(distance):
    while distance > 0: # loop this while there's still distance 
        if turtle.distance(0,0) > 100: # check if the distance from the origin is greater that 100
            angle = turtle.towards(0,0) # find the angle for the origin from turtle's current location 
            turtle.setheading(angle) # turns the turtle toward the angle
        turtle.forward(1) # move title forward in the current angle by one pixel 
        distance = distance - 1 # act as a counter for the while loop, so when distance is finally 0, the while loop breaks

代码与它试图实现的目标有点矛盾。看起来,
向前
功能正在尝试以
距离
步数向位置
(0,0)
移动

def forward(distance):

    while distance > 0:               

        # if the turtle is more than 100 distance away from (0, 0)
        # change orientation to face (0, 0)               
        if turtle.distance(0,0) > 100:
            angle = turtle.towards(0,0)
            turtle.setheading(angle)

        # Make the turtle move one step towards (0, 0)
        turtle.forward(1)

        # IMO, distance should've been called steps_left 
        distance = distance - 1
逻辑不一致性


代码似乎试图使海龟朝着原点移动,但同时它有一个称为距离的计数器,其作用更像是允许海龟朝原点移动的步数。在这个场景中,
turtle.forward(1)
实际上会使海龟离开原点移动一步,在下一个循环中,海龟会移回原点。没有意义。

此函数替代了
turtle.forward()

考虑函数的简化版本:

def forward(distance):
    while distance > 0:
        turtle.forward(1)
        distance = distance - 1
它做的正是海龟前进(距离)
所做的,尽管效率较低。它一次向前移动海龟一个像素,直到它走完整个距离,而不是通过
turtle.forward(distance)
以一个动作走完整个距离。为什么?这允许代码对移动的每个像素做出决定,判断海龟是否违反了边界,并调整其航向:

    if turtle.distance(0, 0) > 100:
        angle = turtle.towards(0, 0)
        turtle.setheading(angle)
让我们将此函数嵌入到代码中,以说明其功能:

from random import randint
import turtle

def forward(distance):
    while distance > 0:
        if turtle.distance(0, 0) > 100:
            angle = turtle.towards(0, 0)
            turtle.setheading(angle)
        turtle.forward(1)
        distance = distance - 1

boundary = turtle.Turtle(visible=False)
boundary.color("red")
boundary.penup()
boundary.sety(-100)
boundary.pendown()
boundary.circle(100)

turtle.shape("turtle")  # default turtle

for _ in range(1000):  # akin to while True:
    turtle.left(randint(0, 90))
    forward(randint(1, 50))

turtle.done()
输出


谢谢@abccd。我理解代码是如何工作到最后一行的:distance=distance-1我刚刚注意到我的部分评论被截断了。我想说的是,我无法理解最后一行的逻辑。所以如果我把数字110作为距离,乌龟会在92点结束。我只是不明白为什么最后的距离是距离-1。正如EyuelDK所说,我正在尝试寻找其他选项。正如我所说的,
distance=distance=1
是一个计数器,整个循环与
for uuuu范围内(distance):
完全相同。乌龟将继续移动,直到柜台结束。这意味着它将前后移动直到循环停止,一次一个像素。谢谢@abccd。我将
用于范围内(距离)
,这对我来说很有意义。不需要额外的
线路距离=距离-步骤
。所以我不确定while循环在这里是否是一个好的选择。我更倾向于使用for循环的后一个选项。除非你想使用/更改距离,但我认为没有必要。两人都很好。非常感谢@EyuelDK。我也对abccd的答案发表了评论。我很难理解作为练习给出的代码的逻辑。这个练习的目的是防止海龟超过设定的距离。如果超过100,我们应该让它停下来,回到100之前,不一定要一直到起点。是的,可变距离令人困惑,因此我将其替换为steps_left-但这对我来说仍然没有意义:为什么最后两行代码应该是turtle.forward(1)和steps_left=steps_left-1?不必是,但它是。在编程中,有很多方法可以完成同样的事情,所以不要认为这是必须的,而是一种选择。在您澄清之后,即告诉我最初的目的,我对代码有了更多的理解。你可以把这个程序想象成是在一个半径为100的圆内移动乌龟,而乌龟的中心在一个设定的步数内,即距离。最后两行使海龟朝着圆圈的中心前进,距离=距离-1更新了允许的步数。谢谢@cdlane。这很有趣,正如我对abccd答案的评论一样,在这种情况下,使用
for uuu-in-range
loop可以更好地理解代码。