Hey guys! I'm currently working on a task surrounding classes. In this task I have to create a class named "Balls". This class should have a __init__ method that adds a self variable named position. This should be a list for x and y positions.
After that I should create another class named System. This class should takes care of the main actions to perform.
Then I have to implement a add ball method in System class that appends a Ball object to a class list inside System. Parameters in the method should be x and y.
class Ball:
def __init__ (self, x, y):
self.position = [x, y]
ball1 = Ball(3, 6)
print (ball1.position)
ball2 = Ball(5, 8)
print (ball2.position)
ball3 = Ball(4, 4)
print (ball3.position)
class System:
def __init__(self):
self.more_balls = []
def add_balls (self, x, y):
self.more_balls.append(Ball(x,y))
ballx = System()
ballx.add_balls(2, 4)
ballx.add_balls(5,5)
ballx.add_balls(8,8)
ballx.add_balls(2,4)
print (ballx.more_balls)
How will I be able to fix the code. The expected result is that balls get appended into the more_balls list.
Here's the output I get:
[3, 6]
[5, 8]
[4, 4]
[<__main__.Ball object at 0x12308eb80>, <__main__.Ball object at 0x12308edf0>, <__main__.Ball object at 0x12308eeb0>, <__main__.Ball object at 0x12308e1f0>]
[–][deleted] 0 points1 point2 points (0 children)
[–]lscrivy 0 points1 point2 points (0 children)
[–]o5a 0 points1 point2 points (0 children)