Flattening Nested if Statements
It’s not uncommon for one
if statement to be inside another, but sometimes programmers do this just to create a second condition for what might be a single case, as in this example here First, it wants to see if a provided number is bigger than
Then it wants to see if it’s less than
10. The only time it arrives at the calculation part is if both conditions were true. So instead of writing it as nested ifs, which like this isn’t considered the best way of writing code in Python, it’s better to write it as a single
and and connecting the two original conditions.
Become a Member to join the conversation.