LCCS-Programming-4-1-conditionalsIf
LCCS-Programming-4-1-conditionalsIf
in Python
Concept 4
Conditionals
(if statements)
Learning Intentions
From this lesson the students will:
1. Understand the concept of a conditional if
statement.
2. Read and Write code to carry out simple tasks.
3. Use relational operators to make comparisons.
if Statement
• if statement: executes a set of commands only if
a certain condition is True.
• Otherwise, the commands are skipped.
Syntax:
indentation
if Statement
• You can execute as many lines of code within an
if statement as you wish.
if Statement
• if (condition is True):
– Execute these set of commands
• Otherwise the commands are skipped.
Syntax
if condition:
execute statements (note indentation)
if flowchart Flow Chart starts here
Execute
Executethese
your
lines
code
Continue
Multiple if statements
Can you predict
what the
output will be ?
LO 1.22 students should be able to read, write, test, and modify computer programs
output from Multiple if program
LO 1.1 students should be able to describe a systematic process for solving problems
and making decisions
Reminders on User Inputs
• Python takes all input as STRINGS.
• You must CAST the input to the DATA TYPE you
want.
• The example below casts the user input from a
string to an integer.
Comparing User Inputs .. sample code
User Interface
• You must tell the user if they guessed correctly and
congratulate them.
• If they guessed incorrectly, you must tell them if their guess
was too high or too low. And let them have 1 more go. If they
are wrong, then say Hard Luck!!!!
LO 1.16 students should be able to compare two different user interfaces and identify
different design decisions that shape the user experience
pseudo code .. for guessing game
generate a random number between 1 and 5;
userNumber = user’s first guess;
if userNumber == gameNumber { print congratulations; }
if userNumber > gameNumber { print your guess is too high; }
if userNumber < gameNumber { print your guess is too low; }
if userNumber != gameNumber {
userNumber = user’s second guess; NESTED if
if userNumber == gameNumber { statements
print congratulations; }
if userNumber != gameNumber {
print Hard Luck; }
}
LO 2.5 students should be able to use pseudo code to outline the functionality of
an algorithm
Lesson Review
As a result of this lesson I am able to: