Lesson 4

If, Elif and Else Statements


If statements are conditions that only execute lines of your program if their parameters are true, similar to the while(true) loop.

if(): statements are sometimes paired with an else: statement that will run all other times

Between if():, and else: there can be any number of elif(): statements, which can check for other things before going to the else

First, we will need to double back through our code to where we added our inputs on lines 8-10. On line 11 add the following code program=input(“What program do you want to run? “)

We will also need to add some code into the forloop that we set up on line 17. On line 18, add the following code: if(program == “octoSpiral”):

This conditional statement will only run if the user enters “octoSpiral” when prompted for the program to run.

Setting up additional Inputs


So far we have only one conditional statement set up. We will want to add in at least two more, so that we can have a bit of variety.

Copy the code shown on lines 21-26 of the image to the side. Rember that matching the case is very important

Once we have out 3 conditional statements set up, we want to set up a fourth option, for when none of the 3 we set up are selected. We will accomplish this by making use of the else

On line 27 add the following code: else:. Make sure that it is on the same indentation as our elif

On line 28, inside of our else: statement, add the following code: t.circle(length)