Warm tip: This article is reproduced from serverfault.com, please click

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

发布于 2020-11-28 00:28:27

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

Questioner
Silya
Viewed
0
Aaj Kaal 2020-11-28 08:53:15

Code:

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

Output:

 *+*
 ###