0% found this document useful (0 votes)
18 views

LCCS-Programming-4-1-conditionalsIf

This document teaches the concept of conditionals in Python, specifically focusing on if statements and their syntax. It covers how to execute commands based on conditions, the use of relational operators for comparisons, and includes examples and exercises such as comparing user inputs and creating a guessing game. The lesson aims to help students understand and apply these concepts in programming.

Uploaded by

todayepisode2020
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)
18 views

LCCS-Programming-4-1-conditionalsIf

This document teaches the concept of conditionals in Python, specifically focusing on if statements and their syntax. It covers how to execute commands based on conditions, the use of relational operators for comparisons, and includes examples and exercises such as comparing user inputs and creating a guessing game. The lesson aims to help students understand and apply these concepts in programming.

Uploaded by

todayepisode2020
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/ 16

Learning to Program

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

All three if statements were true so


all three commands are executed.

LO 1.4 students should be able to solve problems using skills of logic


Testing 2 conditions in 1 if
Read this code.
Especially the and operator.
Predict what happens.

3 temperatures will slip


through?
One of them is 40.
Fill in the Operation and Answer columns for each comparison
- Describe the operation in your own words-
Relational True / False Operation Answer
Operator (True/False)
== 1 + 1 == 2 equals True
!= 3.2 != 2.5 does not equal True
< 10 < 5 less than False
> 10 > 5 greater than True
<= 24 <= 13 less than or
equal to
False
>= 5.0 >= 5.0 greater than or
equal to
True
Comparing User Inputs
Write a program that compares two
numbers entered by the user.
• If the numbers are different, tell the user which
number is greater.
• If the numbers are equal, tell the user the
numbers are equal.
• Before you begin, some reminders…….

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

What code might be behind me?


Create Your Own … Guessing Game
Your task is to write a program that asks the user to guess a
number between 1 and 5 that your program randomly generates.
You must write the algorithm first and then code it in Python.

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:

1. Understand the concept of a conditional if.


2. Read and Write code to carry out simple
tasks.
3. Use relational operators to make
comparisons.

You might also like