Python Python Collections (2016, retired 2019) Dungeon Game Cartographer

Questions about the draw_map func

Hi,

def draw_map(player):
    print(" _" * 5)
    tile = "|{}"
    for cell in CELLS:
        x, y = cell
        if x < 4:
            line_end  = ""
            if cell == player:
                output = tile.format("X") # represent the player, The x is the player
            else:
                output = tile.format("_")
        else:
            line_end = "\n"
            if cell == player:
                output = tile.format("X|") #this represent the right wall(>)
            else:
                output = tile.format("_|")
        print(output, end = line_end)

1.we use line_end = "\n" because if " x<4" we are out of the range of our CELLS map and then this command tells python to go down? >>> I have tried to remove this line and i saw that the map become a long line.

2.I also dont fully understand what this last line does:

    print(output, end = line_end)

from where this "end" come from? i watched the video few times i still dont understand the role of this command.

3.he uses format in a strange way, how he can use output = tile.format if we already declare tile before we always do "{}".format(something) and not declare the varilable as well

i will appricate ur help! :D

1 Answer

Steven Parker
Steven Parker
182,492 Points

It sounds like you've answered question 1 for yourself by doing a little experiment and seeing how it affected the output. Good job! :+1: Yes, the "\n" means "go to the next line".

For your question 2, "end" is an extra parameter for the "print" function. It tell's it what to do at the "end" of printing the first argument. So, based on the setting it will either go to the next line (when it is "\n"), or do nothing special (when it is "").

For question 3, "tile" contains what might be called a "template string". It has a placeholder (the "{}" part) that shows the "format" function where to put the contents of the argument passed to it. The argument is always something to represent what is in (or not in) the cell, and sometimes an extra wall.

Does that clear it up?

Yes, Thank you!