Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
MOD
Chris Freeman
Treehouse Moderator 67,622 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)?