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 trialRaduica Sebastian
Courses Plus Student 1,356 PointsCan someone please explain me the draw_map function?I have difficulties trying to understand the way it is written.
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")
else:
output=tile.format("_")
else:
line_end="\n"
if cell == player:
output = tile.format("X|")
else:
output=tile.format("_|")
print(output, end=line_end)
3 Answers
Flore W
4,744 PointsHey, I have found it easier to understand if you actually take each cell in CELLS, and start drawing on a piece of paper as defined by the code.
CELLS = [(0,0), (1,0), (2,0), (3,0), (4,0),
(0,1), (1,1), (2,1), (3,1), (4,1),
(0,2), (1,2), (2,2), (3,2), (4,2),
(0,3), (1,3), (2,3), (3,3), (4,3),
(0,4), (1,4), (2,4), (3,4), (4,4)]
player = (0,1)
You start by drawing 5 x " _" which gives you the upper end of the map. So you have a single line composed of 5 small _. It looks like this:
_ _ _ _ _
The first item is (0,0). So we are in the case where x < 4 (0 < 4), and (0,0) is different from (0,1) (player). The code reads:
- line_end ="" that means after this first item is printed, the next item will stay on the same line (as opposed to a line break)
- output = tile.format("_") meaning what you draw for position (0,0) is a |_ . Your map should resemble:
_ _ _ _ _
|_
The next item is (1,0), so again 1 < 4 and (1,0) different from (2,3). If you apply the same logic, your map should resemble:
_ _ _ _ _
|_|_
... same for (2,0), (3,0) ...
Let's look at when you reach the last item with y =0. For (4,0), we are in the case where x > 4, and (4,0) is not (0,1). The code reads:
- line_end = "\" that means after this we go to the next line instead of continuing horizontally.
- output = tile.format("_|") this is to close the right hand side of the map, otherwise it would be open like |_ instead of |_|
Now your map should resemble:
_ _ _ _ _
|_|_|_|_|_|
When we reach the player at (0,1), we are in the case where 0 <4, so:
- line-end =""
- output = tile.format("X") (that's basically |X) Note as in Kenneth' video, there is no line under the X.
Your map would resemble:
_ _ _ _ _
|_|_|_|_|_|
|X
So on for the rest of the tuples in CELLS.
Hope you understand the logic better now. It is always a good thing to start drawing the map by hand, and back engineer how you would execute it in code.
Ali Dahud
3,459 PointsAnd I Would like to add to this awesome explanation that after line_end='\n':
there's also an if-else statement which means IF your player is located on the 4th x draw a tile.format(X|) which looks like this |X|.
Kushagra Patel
7,740 Pointsthis explanation really helped
Kevin Toruño
3,942 PointsKevin Toruño
3,942 PointsThank you so much bro