PYTHON PROGRAMMING Year 9
Objective and Outcome Teaching Objective Today we will look at conditional statements in order to understand how programs can become more intelligent Learning Outcome All – of you will have written a simple IF statement and executed a program with a condition Most – of you will be able to use comparative operators in an IF statement Some – of you will be able to run IF, elif programs
Today’s lesson Computer programmers can make programs that are so intelligent they can make decisions which can have a direct bearing on our lives Critical systems such as planes on computers must react to conditions OR simple programs like a login for a computer IF Elif Else
PseudoCode The syntax of a simple IF statement IF a < b Do something IF a > b Do something This is how a program would be written in PSEUDO code Pseudo code is the program written in a form close to English It will not run as it needs the correct syntax
A simple IF program If a is greater than b (>) If a is less than b (<)
A simple IF program This is the output to the program
The syntax explained Input the two variables Note the colon Print if a is the biggest Print if a is the smallest
Exercise 1 (5 minutes) Write a program that asks for two user’s names and ages and then returns a message to say which one is older Instructions (Algorithm) IF userage1 > userage2 PRINT “Message……” IF userage1 < userage2 PRINT “Message…….”
Extend the program The program we have just written does not work if they are the same age. We need to add another IF statement to overcome this Remember (==) is the sign for equal to Algorithm IF userage1 > userage2 PRINT “Message……” IF userage1 < userage2 PRINT “Message…….” IF userage1 == userage2 – PRINT “Message ……”
Check Point What we have done: We’ve created a program which is non – linear It is selective and will behave differently with input We have used an IF statement We have used three operators – (>) greater than – (<) less than – (==) equal to
Program: temperature.py Write a program that takes in the temperature from the user in Fahrenheit PSEUDO CODE IF temperature is greater than 90 – PRINT (“It is hot outside”) ELSE PRINT (“It is cold outside”) PRINT (“Program done”)
Program: Teenager Write a program that uses an IF statement. It does the following: Takes in the user’s name Takes in the user’s age If they are LESS than 13 they are still a child If they are 13 or greater(>=) they are a teenager
Check Point Now we have extended our program so that we have an IF Else statement This gives us much more control over our program In a way our programs are starting to behave more intelligently
Plenary Today we have looked at the power of an IF statement IF is a small word which is very powerful Can we think of examples of how it might be used? We have also looked at comparative operators such as ==