0% found this document useful (0 votes)
4 views27 pages

Lesson 4 - If Statements_Workbook

This lesson focuses on IF statements in Python programming, emphasizing their importance for controlling program flow and evaluating expressions using comparison operators. Students will learn to create IF statements, convert numbers to integers, and identify and fix errors in code. The lesson also includes practical tasks such as writing a simple program for a speed camera and a guessing game.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views27 pages

Lesson 4 - If Statements_Workbook

This lesson focuses on IF statements in Python programming, emphasizing their importance for controlling program flow and evaluating expressions using comparison operators. Students will learn to create IF statements, convert numbers to integers, and identify and fix errors in code. The lesson also includes practical tasks such as writing a simple program for a speed camera and a guessing game.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

Lesson 4

IF Statements

Python Programming
Learning & Skills Objectives
 To understand the importance of having control of our programs in
Python, such as IF statements.

 To learn how to evaluate expressions in Python using the


operators >, < and =.

 I can use an IF Statement correctly.

 I can convert a number to integer format.

Keywords
Control, evaluate, expressions, statements
Lesson Success Criteria

Remembering  I know what an IF Statement is used for in regards to


programming.
 I know how to convert a number to integer format.
Understanding
 I can add comments to my program.

Applying
 I can spot errors in code, explain why they are wrong and I can
fix them.
Analyzing  I can create a simple IF Statement from scratch in a program.
 I can write a piece of code that will convert an number into an
integer.
Evaluating

 I have completed ALL of the tasks in today’s lesson


Creating INDEPENDENTLY.
 I have completed ALL of the extension tasks in today’s lessons.
 I can explain what a structured program is.
Mini Task One

Open Python online

Enter the following code carefully, then run

print("What is 2 + 2?")
answer = input ()
answer = int(answer)
if answer = = 4:
print("Well done")
else:
print("Sorry the answer was 4")
You can add more questions to your game if you like.
Paste code and results in the next slide
print("What is 2 + 2?")
answer = input ()
Mini Task One
answer = int(answer)
if answer = = 4:
print("Well done")
else:
print("Sorry the answer was 4")

Paste your code below: Paste the results below:


l o ok at t he code
…a closer
Mini Task One Review

 You have just written your first structured program.

 The IF statement is a very, VERY useful structure to know.

 When typing it into Python, be very careful not to forget the little
colons :

if answer = = 4 :
print("Well done")
else:
print("Sorry the answer was 4")
l o ok at t he code
…a closer
Mini Task One Review

print("What is 2 + 2?")
Anyone who did the
answer = input () extension task last
answer = int(answer) lesson would have
looked at this!

This is an assignment statement just you have been using


before.

However, you’ll notice that the variable ‘answer’ is


surrounded by brackets and the ‘int’.

Int stands for integer.


l o ok at t he code
…a closer
Mini Task One Review

print("What is 2 + 2?")
answer = input ()
answer = int(answer)

 An integer is just any number without a decimal place.

 Examples of integers are 75, 2, 100, 55, 16.

 12.3 is NOT an integer!

 Therefore int(answer) converts the variable answer to an integer


which is important!
l o ok at t he code
…a closer
Mini Task One Review

if answer == 4:


You’ve probably never seen this before! ==
 A double-equal sign simple means ‘is it equal to?’.
 e.g. if my_school == ‘WHSG’:
reads as if my school is equal to WHSG


A single-equal sign is only used for assignment. =
 e.g. my_school = ‘WHSG’:

reads as The variable my_school is assigned WHSG
l o ok at t he code
…a closer
Mini Task One Review

if answer == 4:
print("Well done")
else:
print("Sorry the answer was 4")

 The above section of the code is understood as follows:

IF the answer is equal to 4 THEN


print ‘Well Done’
ELSE
print ‘Sorry the answer was 4’
Comparison Operators

Operator Meaning Example Evaluates to

== equal to 7==7 True

!= not equal to 6!=7 True

> Greater than 7>6 True

< Less than 5<8 True

>= Greater than or equal to 6>=8 False

<= Less than or equal to 7<=7 True


 Look at the program below. For some reason I can’t get it to
run!!!
Test me!
 Can you spot 7 mistakes made?

print(“Please enter your name: “)


my_name = input ()
print(my_name + ” what age are you”)
my_age = input()
my_age = int(my_age)
if my_age > 11 :
print(“You must go to Secondary school”)
Else :
print(“You must go to Primary school”)
Write your answers here:
Test me ... answers!

Look at the program below. For some reason I can’t get it to


run!!!

Can you spot 7 mistakes made?

print('Please enter your name: ')


my_name = input()
print(my_name+ ' what age are you')
my_age = input()
my_age = int(my_age)
if my_age > 11:
print('You must go to Secondary school')
else:
print('You must go to Primary school')
Review!
Learning Objectives Success Criteria
 To understand the importance Level 5
 I know what an IF Statement is used for in
of having control of our regards to programming.
programs in Python, such as IF  I know how to convert a number to integer
format.
statements.  I can add comments to my program.

Level 6
 To learn how to evaluate  I can spot errors in code, explain why they are

expressions in Python using wrong and I can fix them.


 I can create a simple IF Statement from scratch
the operators >, < and =. in a program.
 I can write a piece of code that will convert an
number into an integer.
 I can use an IF Statement
Level 7
correctly.  I have completed ALL of the tasks in today’s
lesson INDEPENDENTLY.
 I can convert a number to  I have completed ALL of the extension tasks in
today’s lessons.
integer format.  I can explain what a structured program is.
Write a program

Write a simple program that might be used


inside a police speed camera
The Requirements are:
– It must accept the driver’s
speed
– IF the speed is over 70mph, a
message “Issue Fine” should
appear on the speed gun
– Otherwise it should display
“No Action”
Write a simple program that might be used
inside a police speed camera
Paste your code below: Paste the results below:
The ELIF statement

If gives you two if grade >= 80:


options
ELIF stands for print("Distinction")
Else, If and gives elif grade >= 70:
you more options print("Merit")
You can use it as elif grade >= 60:
many times as print("Pass")
you like else:
print("Fail")
Using the ELIF statement

Add an ELIF
statement to the
Speeding program
Issue a warning
between 70 and
75mph
Only issue a fine for
75mph and over
Add an ELIF statement to the Speeding program
Usingbetween
Issue a warning the ELIF statement
70 and 75mph
Only issue a fine for 75mph and over
Paste your code below: Paste the results below:
Did you do it?
Using the ELIF statement

Paste your code below:


Main Task One
If you can, do all these programs in SCRIPT mode
(i.e. go to FILE, NEW WINDOW…then press F5 to run)
Task: Guess the number between 1 & 10 Game.
Create a program in python that prompts the user for a number between 1 and
10. The user’s guess, which could be assigned to a variable user_guess,
should be compared to the number variable my_number which will be set by
the programmer (you).
If they guess the correct number, a ‘well done’ message should
appear. If they guess wrong, a ‘sorry, better luck next time’ message
should appear.

Example…

Welcome to Guess the Number game!


Please enter your guess (between 1 and 10):
7
Well done! You guessed correctly!

PASTE CODE AND RESULTS ON NEXT SLIDE


Main Task One
Paste your code below: Paste the results below:
Extension – Random Number?!

What about if you wanted my_number to be a random


number which changes every time the program is run?
Always being the same number is a bit boring!!

We can use an import function:

See if you can work out how to make my_number be


a random number between 1-10 each time the
program is run!
Extension – Random Number?!

Paste your code below: Paste the results below:


Did you do it?

You could quite simply replace the number in the variable my_number with the
Following code:

import random

my_number=random.randint(0,10)
user_guess=int(input(“Enter your guess: “))

if my_number == user_guess:
print(“Well Done!”)
else:
print(“ Sorry, better luck next time!”)
Review!
Learning Objectives Success Criteria
 To understand the importance Level 5
 I know what an IF Statement is used for in
of having control of our regards to programming.
programs in Python, such as IF  I know how to convert a number to integer
format.
statements.  I can add comments to my program.

Level 6
 To learn how to evaluate  I can spot errors in code, explain why they are

expressions in Python using wrong and I can fix them.


 I can create a simple IF Statement from scratch
the operators >, < and =. in a program.
 I can write a piece of code that will convert an
number into an integer.
 I can use an IF Statement
Level 7
correctly.  I have completed ALL of the tasks in today’s
lesson INDEPENDENTLY.
 I can convert a number to  I have completed ALL of the extension tasks in
today’s lessons.
integer format.  I can explain what a structured program is.
To end with ...

Today you have learnt about the IF statement.

This is a very common function in computer


programming.

Can you come up with other examples of when


the IF statement might be useful?

For example in a game:


IF lives == 0 then game over else continue

You might also like