When might someone absolutely have to use a nested conditional? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

When might someone absolutely have to use a nested conditional?

I’m reading Think Python and it is talking about nested conditionals and chained conditionals and in the text it says to avoid nested conditionals where you can and rewrite it with logical operators so that it’s less confusing. Which brings me to my question… in what sort of situation is using nested conditionals unavoidable? Can anyone provide an example?

7th Jul 2021, 7:12 AM
Jocelyn
Jocelyn - avatar
2 Answers
+ 5
if a: if b: print("a & b are true") else: print("a is true but b is false") else: if b: print("a is false but b is true") else: print("a & b are false") you may see by yourself when you cannot avoid nesting conditonal or simplify the conditional by using 'and' or 'or' in conditions...
7th Jul 2021, 7:59 AM
visph
visph - avatar
+ 1
Thank you both! I think I understand it now.
7th Jul 2021, 9:01 PM
Jocelyn
Jocelyn - avatar