I Haz Codes

Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors
Menu
ELIF

Syntax

if
a
>
59
:
 
print 'True block'
elif
a
>
80
:
 
print 'runs only if the second condition evaluates to true'
else
:
 
print 'False block'

About elif

elif allows you to chain together multiple if statements. It will only ever run one of the statements which is different to having separate if statements where multiple ones could be triggered.

 

 

Here is the code from the video. Try this code out for yourself.

age = int(raw_input("how old are you?"))
if age < 0:
	print "error"
elif age == 1:
	print "baby"
elif age < 4:
	print "toddler"
elif age < 10:
	print "young child"
elif age < 17:
	print "teen"
elif age < 60:
	print "adult"
else:
	print "OAP"

Helpful links

http://www.pwnict.co.uk/webpages/python/syntax/lesson%205/lesson5.html

http://pentangle.net/python/handbook/node30.html

http://www.wellho.net/resources/ex.php4?item=y103/if4.py

Test your skill

1. Try the grade calculator task on the HackIT website.

Task 1: Hint SelectShow

2. Try the leap year task on HackIT.

Task 2: Hint SelectShow

Leave a Reply

Your email address will not be published. Required fields are marked *

Information