You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Python turtle库计算到中心点距离运行无输出问题求助

问题原因
  • 仅定义了距离计算函数calculateDistance,没有实际调用该函数,打印逻辑不会执行,因此没有输出
  • 没有获取turtle随机移动后的当前坐标,无法为计算函数传入可用的参数
修复后的代码
import math
import turtle
import random

def randomTurtle():
    for count in range(10):
        choice = random.randint(1,2)
        if choice == 1:
            turtle.forward(random.randint(5, 50))
        elif choice == 2:
            turtle.right(random.randint(1,359))

def calculateDistance(x,y):
    dist = math.sqrt((0 - x)**2 + (0 - y)**2)
    print(dist)

randomTurtle()
# 获取turtle当前坐标传入计算函数
calculateDistance(turtle.xcor(), turtle.ycor())
# 可选:保持绘图窗口不闪退
turtle.done()
说明

调用turtle.xcor()、turtle.ycor()可以分别拿到turtle当前位置的横、纵坐标,传入计算函数后即可得到与初始中心点的距离,执行打印逻辑后就会在控制台输出结果。

内容的提问来源于stack exchange,提问作者charles wn

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.28 04:15:08