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
Dan B
6,155 PointsWhy doesn't the teacher just use "elif" in this video?
In this video: https://teamtreehouse.com/library/branch-and-loop
We make a ticket machine. Skip to the end of the video and see the code.
Please answer why the teacher does not use "elif" after the first "if" statement, and instead he chooses to use "else:"
I used elif instead and the program runs and completes fine. Also it makes my final program require less writing.
Why did the teacher use "else:"? Is it a better choice than "elif" in this situation?
1 Answer
KRIS NIKOLAISEN
54,974 PointsCan you post your code? I would say typically an else would require less code since there is no condition to consider. If we are talking about the same thing then anything but y or Y would be handled for 'should proceed' by else.
Dan B
6,155 PointsDan B
6,155 PointsThanks it makes sense. Because he used "else:" he didn't have to write the same code twice to account for "n" or "N" being an answer as literally anything other than "y" or "Y" is treated as a false boolean. Saves time and even accounts for typos.
I appreciate the explanation.
P.S. My code was almost the same as his except I just used "elif" and repeated the "if" statement but as you said neither the teacher or I would have needed to if I had just used "else:" instead.