Google Colaboratory: AttributeError: module 'ColabTurtle.Turtle' has no attribute 'circle'

Google Colaboratory: AttributeError: module 'ColabTurtle.Turtle' has no attribute 'circle'

我有一些乌龟作业。但我没有让 circle 命令起作用。我真的很想继续使用 google colab。

(我发现其他帖子也有同样的问题,但他们更改 library/file 名称的解决方案对我不起作用)

(我也尝试过不同的导入方式和名称,并创建了一个新文件,都是导致该错误的原因)

!pip3 install ColabTurtle
import ColabTurtle.Turtle as t
t.initializeTurtle() 

t.forward(25)
t.left(45)
t.circle(70)

forward 和 left 有效,但 t.circle(70) 导致错误: AttributeError: 模块 'ColabTurtle.Turtle' 没有属性 'circle'

这是一张 imgur 截图:https://imgur.com/jvGjwwt

这里是 link 所以你可以在在线文件中试一试:https://colab.research.google.com/drive/1WzSV6ZotxMg85BMeiuc8W5Xq3wiYxwev

是否向您提供了任何文件?据我所知,ColabTurtle 没有 circle() 方法,错误消息是正确的。查看Turtle.py源码,turtle相关的方法有:

forward(units)
backward(units)
right(degrees)
face(degrees)
left(degrees)
penup()
pendown()
speed(speed)
setx(x)
sety(y)
getx()
gety()
goto(x, y)
showturtle()
hideturtle()
bgcolor(color)
color(color)
width(width)

但是没有circle()。这不是 Python 附带的 turtle.py 库,它有一个 circle() 方法和许多其他方法。甚至不是一个适当的子集。

然而,这并不意味着您不能画圆,您只需要根据您拥有的海龟方法定义代码即可。这是我的猜测,尽管我无法对其进行全面测试:

import ColabTurtle.Turtle as t

def polygon(length, n):
    for _ in range(n):
        t.forward(length)
        t.left(360 / n)

t.initializeTurtle()

polygon(10, 60)

圆函数在 google colaboratory turtle 库中不可用。 我在 cdlane 的功能的帮助下重新创建了圆圈:

circle 应该只用给定的半径画一个圆

!pip3 install ColabTurtle
import ColabTurtle.Turtle as t
t.initializeTurtle()

from math import pi

def tcircle(radius):

    #function could be summarized into:
    #regular_polygon(int((2 * pi * radius)/9)),9)

    #explained step by step:
    """draws a regular polygon of n sides
    that is supposed to appear like a circle.
    n is set to 9 for fast drawing time.
    it calculates rounded side length from n and radius"""
    #circumference (c)= 2*pi*radius
    c = 2 * pi * radius


    #n = amount of lines or corners, it defines the accuracy of the circle
    n = 9 # lower number to decrease drawing time (can be any float or int)

    #circumference (c) = ca.  l * n
    #l = length of individual lines 
    l = c / n

    regular_polygon(int(l),n)


def regular_polygon(l, n):
    """draws a regular polygon of n amount sides of length l
    that is supposed to appear like a circle.
    function by cdlane from a Whosebug post"""
    for _ in range(n):
        t.forward(l)
        t.left(360 / n)

#circle_example
t.forward(35)
tcircle(45)

我的解决方案的屏幕截图,circle_example 的样子:https://imgur.com/LXgaB4v