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

Python Python Collections (2016, retired 2019) Dungeon Game Line endings

Siman Shrestha
Siman Shrestha
2,538 Points

Not sure why my code isn't correct.

I don't really understand why Kenneth used end = "" in the video, and I don't get what it does. Please help!

mapping.py
TILES = ('-', ' ', '-', ' ', '-', '||',
         '_', '|', '_', '|', '_', '|', '||',
         '&', ' ', '_', ' ', '||',
         ' ', ' ', ' ', '^', ' ', '||'
)

for tiles in TILES:
    if tile != "||":
        output = tiles
    else:
        output = "\n"
    print(output, end = "")

1 Answer

Chris Evergreen
Chris Evergreen
2,857 Points

A Short Explanation

Hi there!

I first want to mention that you used 'tiles' in one place and 'tile' in another. You would want to fix that.

Let's start by explaining what end= does. In the code, end= followed by a variable, string, etc. would add whatever you want to the end of each printed tile. For example, if you type end="um", then each time the loop prints out a tile, it would be followed by "um". In the challenge, to correct the code, end= should be followed by either a variable holding either "" or "\n" depending on the type of tile.

Alright, so let's use output as the variable name for whatever comes after each tile. I'll use tile instead of tiles in the example below. You would also need to turn "||" into "" (nothing) when it reaches "||", so it doesn't actually print out the "||". Corrected, your code would look something like this:

for tile in TILES:
    if tile != "||":
        output = ""
    else:
        tile = ""
        output = "\n"
    print(tile, end=output)

The above can be read as: If the tile is not a double pipe, then it should be followed by nothing. If the tile is a double pipe, then the tile should be replaced with nothing (erased) and followed by a line break. Print the tile (and line break if the tile is a double pipe), move onto the next tile.

Hope this helps. Best of luck!