Silya
Silya

Reputation: 23

How to print a list of lists with passed elements in a class via join method?

I have code

class Render():
    def __init__(self):
        pass

    def init_screen(self, h, w):
        raise NotImplementedError

    def add_object(self, char, x, y):
        raise NotImplementedError

    def draw_screen(self):
        raise NotImplementedError

    def get_input(self):
        raise NotImplementedError

there is a task: I need to create a ShellRenderer class that will draw the game screen in the console.

-Create a _screen field in it, which will be a list of character lists, size h by w.

-Initialize it with spaces when calling init_screen method.

-The add_object method must change the value of one of the _screen list items to a char character.

-The draw_screen method should print the list by calling print.

-In the get_input method, you can use the input function to get user input. Return a user-entered string.

I have almost done everything:

class ShellRender(Render):
    def init_screen(self, h, w):
        self.h = h
        self.w = w
        self._screen = [[[' '] for i in range(w)] for j in range(h)]

    def add_object(self, char, x, y):
        self._screen[y][x] = char

    def draw_screen(self):
        print("\n".join(map(str, self._screen)))  # here is a mistake...

    def get_input(self):
        return input()

but I cannot print the list of lists in the draw_screen method. I think it is necessary to use "\ n" .join (map (str, ...)) so that when _screen = [['*', '+', '*'], ['#', '#', '#' ]] output should be

*+*
###

But I can't write it correctly. Maybe I didn't understand the task properly, so please help me to fix this problem

Upvotes: 1

Views: 84

Answers (3)

Aaj Kaal
Aaj Kaal

Reputation: 1274

Code:

_screen = [['*', '+', '*'], ['#', '#', '#' ]]
print(*(''.join(str_arr) + '\n' for str_arr in _screen))

Output:

 *+*
 ###

Upvotes: 1

André Ginklings
André Ginklings

Reputation: 381

self._screen is a list of list, when you use map(str, self._screen) you are using str in a list. And there is a extra "[]" when you create screen. This will work.

class Render():
    def __init__(self):
        pass

    def init_screen(self, h, w):
        raise NotImplementedError

    def add_object(self, char, x, y):
        raise NotImplementedError

    def draw_screen(self):
        raise NotImplementedError

    def get_input(self):
        raise NotImplementedError

class ShellRender(Render):
    def init_screen(self, h, w):
        self.h = h
        self.w = w
        self._screen = [[' ' for i in range(w)] for j in range(h)]

    def add_object(self, char, x, y):
        self._screen[y][x] = char

    def draw_screen(self):
        print("\n".join(map(draw_screen_line, self._screen)))  # here is a mistake...

    def get_input(self):
        return input()

def draw_screen_line(screen_line):
    return "".join(screen_line)

Upvotes: 1

IsolatedSushi
IsolatedSushi

Reputation: 152

With map(str, self._screen)) you are transforming the list of the row into a string. Meaning that [1,2,3] would become '[1,2,3]'.

You should join each row before printing it all out.

def draw_screen():
        print("\n".join(map(''.join, (_screen))))

Upvotes: 1

Related Questions