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

huangapple go评论104阅读模式
英文:

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

问题

我有一些乌龟作业,但我无法使圆形命令工作。我真的想继续使用Google Colab。

(我找到了其他帖子,他们遇到了同样的问题,但是更改库/文件名称的解决方法对我无效)

(我还尝试了不同的导入方法和名称,并创建了一个新文件,都导致了这个错误)

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

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

forwardleft 工作正常,但 t.circle(70) 引发错误:
AttributeError: module 'ColabTurtle.Turtle' has no attribute 'circle'

这是一个 imgur 截图链接:https://i.stack.imgur.com/kz2Ji.jpg

这是链接,您可以在在线文件中尝试:https://colab.research.google.com/drive/1WzSV6ZotxMg85BMeiuc8W5Xq3wiYxwev


<details>
<summary>英文:</summary>

*i got some turtle homework. but i did not get the circle command to work. i really want to keep using google colab.*

(i found other posts with the same problem, but their solution to change the library/file name didn&#39;t work for me)

(i also tried different import methods and names, and created a new file, all cause that error)


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

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


forward and left worked, but the t.circle(70) causes the error:
**AttributeError: module &#39;ColabTurtle.Turtle&#39; has no attribute &#39;circle&#39;**

*here is an imgur screenshot: https://i.stack.imgur.com/kz2Ji.jpg*

*here is the link so you can try around in the online file: https://colab.research.google.com/drive/1WzSV6ZotxMg85BMeiuc8W5Xq3wiYxwev*



</details>


# 答案1
**得分**: 1

以下是您要翻译的内容:

**在Google Colab的turtle库中,没有提供圆形函数。**我在cdlane的帮助下重新创建了圆形:

*circle函数应该只使用给定的半径绘制圆形*

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

from math import pi

def tcircle(radius):

    # 函数可以总结为:
    # regular_polygon(int((2 * pi * radius)/9), 9)

    # 逐步解释:
    """绘制一个n边的正多边形,它应该看起来像一个圆。
    n设置为9以提高绘制速度。
    它根据n和半径计算出圆角边的长度"""
    # 周长(c)= 2*pi*半径
    c = 2 * pi * radius

    
    # n = 线段或角的数量,它定义了圆的精确度
    n = 9 # 降低数字以减少绘制时间(可以是任何浮点数或整数)

    # 周长(c)= 大约 l * n
    # l = 单个线段的长度 
    l = c / n
    
    regular_polygon(int(l), n)


def regular_polygon(l, n):
    """绘制具有n个边且长度为l的正多边形,它应该看起来像一个圆。
    由cdlane在stackoverflow上的帖子中的函数实现"""
    for _ in range(n):
        t.forward(l)
        t.left(360 / n)

# 圆形示例
t.forward(35)
tcircle(45)

我解决问题的屏幕截图,圆形示例的外观:https://i.stack.imgur.com/ThxUF.jpg

英文:

the circle function is not available in the google colaboratory turtle library.
i kind of recreated the circle with the help of cdlane's function:

circle is supposed to draw a circle with only the radius given

!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:
    &quot;&quot;&quot;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&quot;&quot;&quot;
    #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):
    &quot;&quot;&quot;draws a regular polygon of n amount sides of length l
    that is supposed to appear like a circle.
    function by cdlane from a stackoverflow post&quot;&quot;&quot;
    for _ in range(n):
        t.forward(l)
        t.left(360 / n)

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

screenshot of my solution, how the circle_example would look like: https://i.stack.imgur.com/ThxUF.jpg

答案2

得分: 0

以下是翻译好的部分:

"你是否提供了任何文档?就我所知,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() 方法和许多其他方法,甚至不是一个完全的子集。

然而,这并不意味着你不能画圆,你只需要根据你拥有的 Turtle 方法来定义代码来实现。这是我对此的猜测,尽管我无法完全测试它:

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)
英文:

Were you supplied with any documentation? As far as I can tell, ColabTurtle has no circle() method and the error message is correct. Looking at the Turtle.py source, the turtle-related methods include:

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)

But no circle(). This isn't the turtle.py library that comes with Python which has a circle() method and many others. Nor even a proper subset.

However, this doesn't mean you can't draw circles, you just need to define code to do so in terms of the turtle methods you do have. Here's my guess at such, though I'm not in a position to fully test it:

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)

huangapple
  • 本文由 发表于 2020年1月7日 01:56:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/59616766.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定