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!

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 Types and Branching Comparisons

Ashwin Kumar
Ashwin Kumar
680 Points

In the last else statement, instead of a nested if statement, can I use an elif and finally close with else?

In the case where we are getting the age from the user, can I use an elif instead of a nested if and finally close with the else statement? Just wanting to know if my thinking is right. Sorry for the noob question.

3 Answers

Chris Freeman
Chris Freeman
Treehouse Moderator 68,404 Points

The issue with removing the nested if would be finding the value of age To branch with. And you want to ask the age only if necessary.

Think of a nested if as a subdivision of work within a division:

  • D1: name is Craig
  • D2: name is Max
  • D3: name is something else. First ask the age:
  • D3-sub1: age is <= 6
  • D3-sub2: age > 6 (Note this is empty since there is no else

Both sub1 and sub2 finish with the same print statement.

Post back if you need more help. Good luck!!!

Ashwin Kumar
Ashwin Kumar
680 Points

So a nested if can also come in the first condition if you want to add additional checking parameters to D1 or D2? Like checking Craig's age (just a scenario to understand)?