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

2

0

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'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 'ColabTurtle.Turtle' has no attribute 'circle'

here is an imgur screenshot: https://i.sstatic.net/kz2Ji.jpg

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

Wilton answered 6/1, 2020 at 17:56 Comment(2)
if you want me to improve my question, i am also happy to have feedback :)Wilton
Apparently the library that you are using does not offer a circle() function. Please explain why you would expect it to offer one.Milden
W
1

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:
    """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 stackoverflow post"""
    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.sstatic.net/ThxUF.jpg

Wilton answered 7/1, 2020 at 14:8 Comment(2)
they could be sumnmarized into 1 function.Wilton
also one thing that bothered me is that the command t.clear() or t.reset() don't work. so, to start with a clean window i go to Runtime > Restart and run all... (runtime is in the top collumn next to file and edit)Wilton
D
0

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)
Dismiss answered 6/1, 2020 at 18:41 Comment(2)
thanks a lot, I really apreciate the clear answer :) it looks like it is the only function that i need that was not included was circle one question, that might sound too basic: where did you find the list of functions?Wilton
@TristanHeck, I got the above list of functions by (Unix) grepping 'def ' on the source file for ColabTurtle/Turtle.py and hand editing the results.Dismiss

© 2022 - 2024 — McMap. All rights reserved.