1

I used a generator to create a 10 by 10 of 'O's:

board = []

for x in range(0, 10):

    board.append(["O"] * 10)

def board_generator(layout):

    for y in layout:

        yield (" ".join(y))

my_board = board_generator(board)

Then I set imputed the result into a variable, but when I tried outputting it to another source, I got this:

<generator object board_generator at 0x0135AAF0>

I tried to get the printed result I wanted by doing this:

def print_board(boards):

    for i in boards:

        print(i)

However, I could not place impute this into a variable because it does not return anything, therefore I could not output it to a text file. How do I fix this?

jpp
  • 159,742
  • 34
  • 281
  • 339
flame boi
  • 41
  • 10

1 Answers1

1

Try my_board = list(board_generator(board)).

A generator is different to a regular function in that it requires you to either iterate its contents (for example, via next) or exhaust it via another function (for example, list or set).

See What does the “yield” keyword do? for more details.

jpp
  • 159,742
  • 34
  • 281
  • 339
  • Thanks, that makes sense but I just realized an easier way of accomplishing what I am trying to do. (Which is build a battleship file that outputs data into a text file for another python file to read) – flame boi Apr 02 '18 at 19:29