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

Print each item on the same line unless the item is a double pipe (||). In that case, instead of printing the item, prin

How do I get it to print out?

Thanks,

mapping.py
TILES = ('-', ' ', '-', ' ', '-', '||',
         '_', '|', '_', '|', '_', '|', '||',
         '&', ' ', '_', ' ', '||',
         ' ', ' ', ' ', '^', ' ', '||'
)
for tile in TILES:
       if tile != "||":
            output = tile
            else:
                output = "\n"
            print( end="")

1 Answer

Hi Karen,

It seems you possibly misunderstood what the question is asking for.

You need to print out each item in the TILES list unless it is '||'. If that is the case you print a new line.

This code works:

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

To explain my code I use a for loop to loop through each list item. I check to see if each index is equal to '||' and if it is just print('\n') (print only a newline character).

Then in my else block I print tile and add one argument. You can see in the Python documentation here: https://docs.python.org/3/library/functions.html#print

That the end argument in python by default is a newline character. Changing this to end='' gets rid of the newline and prints everything on the same line. Thus printing everything out on the same line. Unless of course the current index is == to the '||'.

I hope this was helpful!