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

Andrei Oprescu
Andrei Oprescu
9,547 Points

I have done exactly what the question said but why am I getting it wrong?

Hi!

I have come across a question that is asking me to do this:

OK, here's a...weird...set of tiles. I need you to loop through TILES and print out each item. Print each item on the same line unless the item is a double pipe (||). In that case, instead of printing the item, print a new line (\n). Use the end argument to print() to control whether things print on a new line or not.

my code is at the bottom of the question.

What have I not done to get the right output on this question?

Thanks!

Andrei

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

etile = ""
for tile in TILES:
    if tile != '||':
        etile += tile
    elif tile == '||':
        print(etile, end='/n')
        etile = ""

3 Answers

Steven Parker
Steven Parker
231,269 Points

You have an interesting and rather unconventional solution. The only functional issue is the symbol for newline:

        print(etile, end='\n')  # note: not '/n'

However, that's the default behavior so you could just write:

        print(etile)

This of course completely bypasses the point of practicing control over line endings, but it does pass the challenge! If you want to experience the original intention of the challenge, try printing the tiles individually without collecting them into a string.

Andrei Oprescu
Andrei Oprescu
9,547 Points

Thanks!

I have done what you have told me to do in the challenge and I have also tried the conventional way to do it. My code was this:

TILES = ('-', ' ', '-', ' ', '-', '||',
         '_', '|', '_', '|', '_', '|', '||',
         '&', ' ', '_', ' ', '||',
         ' ', ' ', ' ', '^', ' ', '||'
)

for tile in TILES:
    if tile != '||':
        ltile = TILES.index(tile)
        next_tile = ltile + 1
        if next_tile == '||':
            print(tile, end='/n')
        else:
            print(tile)

It also gives me an error that The output was not correct.

Can you please tell me why?

Thanks!

Andrei

Steven Parker
Steven Parker
231,269 Points

There's a few issues:

  • you still have that slash-n ('/n') instead of the newline code using backslash ('\n')
  • the "index" function only returns the index of the first match
  • a plain "print" with no "end" setting gets a newline by default

Hint: Try it using only each tile itself to determine how to print.

Andrei Oprescu
Andrei Oprescu
9,547 Points

Thank you

I followed your advice on using each tile by itself to print and it worked

Andrei