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<noob />
17,062 PointsQuestions 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
231,236 PointsIt sounds like you've answered question 1 for yourself by doing a little experiment and seeing how it affected the output. Good job! 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?
<noob />
17,062 Points<noob />
17,062 PointsYes, Thank you!