Lesson 4 - If Statements_Workbook
Lesson 4 - If Statements_Workbook
IF Statements
Python Programming
Learning & Skills Objectives
To understand the importance of having control of our programs in
Python, such as IF statements.
Keywords
Control, evaluate, expressions, statements
Lesson Success Criteria
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
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")
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!
print("What is 2 + 2?")
answer = input ()
answer = int(answer)
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")
Level 6
To learn how to evaluate I can spot errors in code, explain why they are
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
Example…
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