Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Integer object error
#1
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()
Reply
#2
typos in line 12 and 44 ?
swallow osama bin laden
Reply
#3
thanks! that is my fault for not thoroughly going over my program.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  error 'ring' is not a callable object JakeWitten 1 3,011 May-02-2017, 05:09 AM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020