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 Basics (2015) Logic in Python If This Then That

Galen McAllister
PLUS
Galen McAllister
Courses Plus Student 982 Points

What happened to the "elif" statement?

Why did the "if" statement print ("Time to retire!") but not the "elif" statement ("Lots of time left!") when both conditions were satisfied? Why weren't they both printed?

I kind of had the same Q. In C++, they both would have printed unless they were nested separately which is kind of what happened here. I do miss nesting, from C based languages.

Our assumption was wrong because the elif statement though located under if statement is not indented therefore it is not part of the block that would be executed when the if statement runs true..

1 Answer

Steven Parker
Steven Parker
231,261 Points

The "elif" condition is only tested if the previous test had failed. If the previous test passes, then after the code in the associated block runs, execution skips down to the end of the conditional chain.

If you want both conditional blocks to run when both conditions are satisfied, just make both tests plain "if" statements (don't use "elif").

Correct!