gpt4 book ai didi

python - 谁能解释这种奇怪的 python 乌龟事件?

转载 作者:太空宇宙 更新时间:2023-11-04 07:26:16 25 4
gpt4 key购买 nike

如果你不知道,python turtle是一个帮助人们学习python的应用程序。您将获得一个 python 解释器和一个屏幕上的海龟,您可以将它们传递给使用 python 的方向。

go(10) 将使海龟移动 10 个像素turn(10) 将使其顺时针旋转 10 度

现在看看这个

alt text

代码:

import random
while(1):
r = random.randint(1,10)
go (r)
r = random.randint(-90,90)
turn (r)

谁能解释一下这种行为?注意直线。 pythons random 模块有问题吗?

最佳答案

调试此类问题时,可能值得在执行时打印出每条指令的值。希望你的乌龟环境有办法将值打印到屏幕上的某个窗口。你可能会这样做:

while(1):
r = random.randint(1,10)
print "going:", r
go (r)
r = random.randint(-90, 90)
print "turning:", r
turn (r)

这项技术有很多名称,但我喜欢的名称是“有疑问时,打印更多”。这样做可以让您深入了解为什么您的乌龟会表现出您所看到的行为。

关于python - 谁能解释这种奇怪的 python 乌龟事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1224944/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com