-2

I'm trying to make a program that draws five balls on my screen using the turtle module in python. I'm trying to use as little lines of code as possible but now i have this attribute error which I don't understand.

import turtle
import random

class turtles:
    def __init__(self):
        self.turtle.Pen()
        self.color(random.randint(0.0, 1.0),random.randint(0.0, 1.0) ,random.randint(0.0, 1.0))
        self.begin_fill()
        self.circle(50)
        self.end_fill()

t1= turtles()

def t1_circle():
    t1.left(90)
    t1.forward(250)
    mycircle(random.randint(0.0, 1.0),random.randint(0.0, 1.0) ,random.randint(0.0, 1.0))

t1_circle()

I expected a ball to be drawn on the screen displaying random colors.

cdlane
  • 40,441
  • 5
  • 32
  • 81
  • 4
    Could you include the error output, please? It usually has useful information for debugging. – Valentino Dec 22 '18 at 13:13
  • 1
    when you instantiate a `turtles` object it's calling `self.turtle.Pen()` but there is no `self.turtle` defined. You need to fix that. – Robin Zigmond Dec 22 '18 at 13:14

2 Answers2

0

You are getting that error because class turtles does not have an attribute 'turtle'. When you specify self.turtle, python expects an attribute named turlte defined in the turtles class. You can read more into this here

I expected a ball to be drawn on the screen displaying random colors.

Here i will just do the above mentioned. You change the code to meet your exact requirements.

import turtle
import random

class turtles:
    def __init__(self):
        turtle.Pen()
        turtle.color(random.randint(0.0, 1.0),random.randint(0.0, 1.0) ,random.randint(0.0, 1.0))
        turtle.begin_fill()
        turtle.circle(50)
        turtle.end_fill()

while True:
    t1= turtles()
nandu kk
  • 368
  • 1
  • 10
0

I'm trying to make a program that draws five balls on my screen using the turtle module in python. I'm trying to use as little lines of code as possible ...

from turtle import *
from random import *

WIDTH, HEIGHT = getscreen().window_width() // 2, getscreen().window_height() // 2

penup()

for _ in range(5):
    goto(randrange(50 - WIDTH, WIDTH - 50), randrange(50 - HEIGHT, HEIGHT - 50))
    dot(100, (random(), random(), random()))

done()

For a filled circle, the dot() command has some advantages. It's always a filled circle so no begin_fill() and end_fill(). You can pass a color right to the dot() function. It prints a circle of a given diameter (not radius) centered on the turtle (not with turtle on the edge like circle().

cdlane
  • 40,441
  • 5
  • 32
  • 81