If statements, or conditionals, start with a test that compares two values. If the test statement is true, the code indented underneath runs. If the test statement is not true, the block of code underneath is skipped and the rest of the program runs.
- Click on the if-else toolkit
- Drag the If block into the editor
- Notice the == This is called a comparision operator. == compares a variable to a value. In this example if the variable "choice" contains the value "yes" then the code under the "if" will run
- The code to run as part of the "if" needs to be indented 4 spaces (same as a tab)
- The "if" code ends once the next line underneath is un-indented.
if choice == "yes": # add your code here
With the If-Else statement the comparison test has two results. If the test statement is true, the code indented underneath the "if" runs. If the test statement is not true, the block of code underneath the "else" runs.
- Click on the if-else toolkit
- Drag the If else block into the editor
- The code to run as part of the "if" needs to be indented 4 spaces (same as a tab) as well as the code under the "else"
- The "if" and the "else" work as a pair so the code to run following the "if-else" pair would be un-indented folllowing the code under the "else"
if choice == "yes": # add your code here else: # add your code here #the code to run after the if-else would be here