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

mapping.py problem

"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."

I have seen many questions regarding this challenge here, and I have followed each and every advice, yet the challenge still isn't passing. It does work as expected on my local machine, though. Here's my code:

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

7 Answers

hello,

since the default for end in print() is a new line, in your else statement you are really calling print('\n', end='\n'), printing two new lines. Maybe in the else just call print().

This is probably a challenge right? there is no link to the actual challenge in the question, usually the site engineers a link so I have not looked at the challenge.

TILES = ('-', ' ', '-', ' ', '-', '||',
         '_', '|', '_', '|', '_', '|', '||',
         '&', ' ', '_', ' ', '||',
         ' ', ' ', ' ', '^', ' ', '||'
)
for tile in TILES:
    if tile != "||":
        print(tile, end="")
    else:
        print()

This is correct, however the Treehouse instructions are misleading: "In that case, instead of printing the item, print a new line (\n)." Referencing the \n suggests to the student that they are to use this in the code.

Thank you james. Now I know how end = works!

what does *** end="" *** do in your code? Does this read "end of line"?

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

Why does *** print(tile, end="") *** need to be in a (x,y) format to work?)

Thanks

Thanks James! Indeed you are right, that was the right answer. Thanks for pointing this print() behaviour out. I completely forgot about it!

thanks james that really worked ,,N/B default for end in print() is a new line.

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

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