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 trialGalen McAllister
Courses Plus Student 982 PointsWhat 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?
1 Answer
Steven Parker
231,261 PointsThe "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").
iamserda
2,672 PointsCorrect!
iamserda
2,672 Pointsiamserda
2,672 PointsI 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..