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 trialDmitri Weissman
4,933 PointsIF order within PRINT() matters ? print (t, end = '' if t != '||' else '\n')
The attached code is not working as for some reason it prints the item anyway regardless of the matching if statement and only then evaluates the if. If i change the
print (t, end = '' if t != '||' else '\n')
to
print ('\n' if t == '||' else t, end = '')
it will work just fine. can someone explain why it works this way ?
TILES = ('-', ' ', '-', ' ', '-', '||',
'_', '|', '_', '|', '_', '|', '||',
'&', ' ', '_', ' ', '||',
' ', ' ', ' ', '^', ' ', '||'
)
for t in TILES:
print (t, end = '' if t != '||' else '\n')
4 Answers
Steven Parker
231,275 PointsIt's not the order but the functionality.
The first example always prints "t", and then ends the print different ways based on the contents of "t". The second example only prints "t" if it is not the "||" symbol.
The second example meets the challenge requirements but the first one does not.
Dmitri Weissman
4,933 Pointsoooh, got it. for some reason I was thinking that everything to the left of IF should meet the condition. so if i change the first version to
print (t if t != '||' else '\n' , end = '')
it will work.
Is there a way (with similar syntax) to have two different END arguments for print() ?
Steven Parker
231,275 PointsSure, your first example did that, but it just produced a different result than the challenge was looking for. Here's a solution that uses the conditional expression syntax for both the content and the end:
print (t if t != '||' else '', end = '\n' if t == '||' else '' )
For illustration only, you already have a better solution.
Dmitri Weissman
4,933 PointsCorrect me if i'm wrong, basically, each argument passed to a function should be evaluated with it's own if ?
Steven Parker
231,275 PointsThat's what my example shows. But it would be very unusual to use a conditional expression on each argument of a function!
Dmitri Weissman
4,933 Pointsgreat, thank you.