Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Object-Oriented Python Dice Roller Boards

boards.py

Now make all Board instances iterable so we can loop through their cells attribute. If you need help, refer back to the Emulating Builtins video.

boards.py
class Board:
    def __init__(self, width, height):
        self.width = width
        self.height = height
        self.cells = []
        for y in range(self.height):
            for x in range(self.width):
                self.cells.append((x, y))


class TicTacToe(Board):
    def __init__(self):
        super().__init__(3, 3)

4 Answers

Chris Christensen
Chris Christensen
9,281 Points

The challenge says to review "Emulating Builtins" but it is simply underlined and not linked. The actual link is https://teamtreehouse.com/library/emulating-builtins and @kennethlove explains it at the 5:05 mark in the video.

Since this challenge is a bit separated from the "Emulating Builtins" video, it was good to go back and watch the video to understand how this challenge is solved.

Moderator Edit: Best Answer selected for this post

Use yield from self.cells when you override the iter method.

Han Xiao
Han Xiao
10,386 Points

just loop through, below works for me def iter(self): for item in self.cells: yield item

code

class Board: def init(self, width, height): self.width = width self.height = height self.cells = [] for y in range(self.height): for x in range(self.width): self.cells.append((x, y))

# Here is the code added            
def __iter__(self):
    for cell in self.cells:
        yield cell

class TicTacToe(Board): def init(self): super().init(width = 3,height = 3)