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

Eva Feng
Eva Feng
1,250 Points

Do you need "else" after if statement

At 7:45 of the video - there are two if statements, however first one uses "else" and the second one doesn't, why is that?

2 Answers

Steven Parker
Steven Parker
243,160 Points

An "else" is optional. Sometimes when a condition is not true, you don't need to do anything, so no "else" is needed.

And even when you do want to respond to a condition not being true, when the "if" code redirects program flow (such as with a "break", "return", or "raise") the "else" isn't necessary.

Eva Feng
Eva Feng
1,250 Points

Thank you Steven!