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

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.

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')
else:
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:
else:
--code to be executed

Comments

author
BlueBlood Studios (author)2014-03-28

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

author

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)

testvalue

>>>

author
wrongname (author)2014-03-27

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

if python_version = "3":

input = "input"

else:

input = "raw_input"

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

About This Instructable

1,213views

7favorites

License:

Bio: 1 new python tutorial every day Comment what you want the next one to be Please favourite and subscribe Notice Board: - since instructables has automatically ... More »
More by BlueBlood Studios:Teach Me Python #7: Control Statements Pt.2: While LoopsTeach My Python #6: Control Statements Pt.1: If, Else And ElifTeach Me Python #5: Simple Maths
Add instructable to: