Teach My Python #6: Control Statements Pt.1: If, Else and Elif

About: This project has expired.

I'm sorry for this tutorial being slightly late as I got busy with my latest iPhone app.

First thing you will ask me is: what are control statements? Well I will answer your question now. Control statements are statements that effect the flow of your code such as saying I will only tidy my room IF my mum is angry. A list of control statements used in python are if, else, elif (else if), while and for. So let's now dive into the world of control statements. As always please favourite subscribe and comment.

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Step 1: The Code

----if, elif and else statements

Type the following code:

favouriteIceCream = input('What is your favourite ice cream? ")
if favouriteIceCream == 'chocolate':
print('That is my favourite flavour too!')
elif favouriteIceCream in ['vanilla', 'malteser', 'strawberry']:
print('I like that flavour')
print('I dont like that flavour')

You should notice that this creates a prompt which asks you what your favourite ice cream flavour is, if the answer is chocolate it says that it's its favourite flavour. While if you enter vanilla malteser or strawberry it says it likes that flavour, if you enter anything else it will say it doesn't like that flavour. How this works is on the 2nd line it is checking if the input is equal to chocolate (this is case-sensitive) then it says if this evaluates to true execute that block of code. To declare an if and an elif statements, the syntax is:
if/elif variable == 'magic!'/in ['pop', 'boom']:
--code to be executed
On the 3rd line you will notice we did it a bit differently, what we do here is we create a list and tell it to execute the code if the input is in the list using an in statement. Finally the else statement is executed if none of the other conditions evaluate to true, there can only be 1 else statement and its syntax is:
--code to be executed



    • Indoor Lighting Contest

      Indoor Lighting Contest
    • Make It Fly Challenge

      Make It Fly Challenge
    • Growing Beyond Earth Maker Contest

      Growing Beyond Earth Maker Contest

    3 Discussions

    BlueBlood Studios

    5 years ago

    Hi wrongname just to say saying input will still work in python 2 but is not as stable as raw_input

    1 reply

    Python 2.6.6 (r266:84292, Oct 12 2012, 14:23:48)

    [GCC 4.4.6 20120305 (Red Hat 4.4.6-4)] on linux2

    Type "help", "copyright", "credits" or "license" for more information.

    >>> test = input("Test value is: ")

    Test value is: testvalue

    Traceback (most recent call last):

    File "<stdin>", line 1, in <module>

    File "<string>", line 1, in <module>

    NameError: name 'testvalue' is not defined

    >>> test = raw_input("Test value is: ")

    Test value is: testvalue

    >>> print(test)




    5 years ago on Introduction

    i think you need to specify it is python 3, for older versions "input" = "raw_input"

    if python_version = "3":

    input = "input"


    input = "raw_input"

    DISCLAIMER: don't use my comment as a code, it is just a joke