Feb-18-2018, 12:12 AM
from turtle import Turtle import random class Shape(): turtle = Turtle() class Circle(Shape): def __init__(self, center, radius): self.center = center self.center = radius def get_area(self): return 3.14*self.radius*self.radius def draw(self): Shape.turtle.penup() Shape.turtle.setposition(self.center[0], self.center[1] - self.radius) Shape.turtle.pendown() Shape.turtle.circle(self.radius) Shape.turtle.penup() def random_shapes(count): def random_point(): return (random.randint(-300,300), random.randint(-300,300)) shapes = [] for i in range(1, count+1): shape_type = random.randint(1, 1) if shape_type == 1: shapes += [Circle(random_point(), random.randint(1,300))] return shapes def main(): shapes = random_shapes(15) total_area = 0 for s in shapes: s.draw() total_area += s.getarea() input ('Hit <enter> key to end.') print ('total area = ', total_area) input ('Goodbye!') main()
Error:line 19, in draw
Shape.turtle.setposition(self.center[0], self.center[1] - self.radius)
builtins.TypeError: 'int' object is not subscriptable
I don't understand this error that comes up in the code above but it does not happen in my other one below:class Circle: from turtle import Turtle t = Turtle() def __init__(self, center, radius): self.turtle = Circle.t self.center = center self.radius = radius def get_area(self): return 3.14* self.radius*self.radius def get_circumference(self): return 2*3.14*self.radius def draw(self): self.t.penup() # Lift pen self.t.setposition(self.center[0], self.center[1] - self.radius) # Position pen to draw rim of circle self.t.pendown() # Place pen to draw self.t.circle(self.radius) # Draw the circle self.t.penup() # Lift pen import random for i in range(0, 20): center = (random.randint(-300,300), random.randint(-300,300)) radius = random.randint(0,300) c = Circle(center, radius) c.draw()