6.11. 👩💻 Setting Up Conditionals¶
Before writing your conditionals, it can be helpful to make your own flowchart that will plot out the flow of each condition. By writing out the flow, you can better determine how complex the set of conditionals will be as well as check to see if any condition is not taken care of before you begin writing it out.
To make sure that your code covers all of the conditions that you intend for it to cover, you should add comments for each clause that explains what that clause is meant to do. Then, you should add tests for each possible path that the program could go though. What leads to certain conditional statements being executed? Is that what you intended?
6.11.1. Choosing your type of Conditional¶
When adding conditionals to your program, you should also consider the kinds of conditionals that are at your disposal and what would fit best.
Though you’ll use them often, remember that conditional statements don’t always need an else clause. When deciding the flow, ask yourself what you want to have happen under a certain condition. For example, if you wanted to find all of the words that have the letter ‘n’ in them. If there’s nothing that needs to happen when a word does not contain the letter ‘n’ then you won’t need an else clause. The program should just continue onward!