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

02 Python Continued - Lesson 2

The document discusses if-else statements in Python and how they allow programs to execute different code depending on conditions. It provides examples of if-else statements and introduces elif statements to allow checking multiple conditions. The document also discusses taking user input and using it in if-else statements.

Uploaded by

innukhan.sweet
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

02 Python Continued - Lesson 2

The document discusses if-else statements in Python and how they allow programs to execute different code depending on conditions. It provides examples of if-else statements and introduces elif statements to allow checking multiple conditions. The document also discusses taking user input and using it in if-else statements.

Uploaded by

innukhan.sweet
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Remembering IF Statements

Introduction
Selection is a programming construct, which allows programs to take different pathways (execute different
lines of code) depending on conditions, and this is what we need to code our programs to make decisions.

Selection and If-Else Statements


Selection is coded using if–else statements. They
work by evaluating a condition and then producing
different outcomes depending on the evaluation.
This may sound complicated but really all that
happens is that they check if something is ‘True’. If it is ‘True’, the program will execute (run) some lines of
code. If it is not ‘True’, the program will run some other lines of code.
Here are two examples of an if-else statement in action.

Example 1
In this example, we have a variable called ‘number’ which contains the number 10. The IF statement checks
to see if the variable is equal to 10. In this example, the variable ‘number’ does indeed contain the number
10 (the condition evaluates to ‘True’), and so the print() statement (indented underneath) is executed (run),
outputting the string ‘The number equals 10’.

Example 2
In this example, the contents of the variable ‘number’ is now 5. This time, when the IF statement checks to
see if the variable is equal to 10, it finds that it doesn’t. This condition evaluates to ‘False’ as 5 does not
equal 10. As a result, the program executes (runs) the line of code indented underneath the ELSE statement,
outputting the string ‘The number doesn’t equal 10’.

Multiple Selection and ELIF Statements


The examples above show us how to code a program to take one of two pathways. However, the reality is
that we will often want our programs to be able to choose from multiple pathways, not just two. Thankfully
ELIF statements provide the solution.
By studying the example below, you can see how they allow us to extend our if-else statements to contain
multiple condition checks.

Notice how after checking if the user entered 1, 2 or 3, there is a final else statement. It is always a good idea
to use this to return an error message in the event that the user did not enter an expected input.
Remember, if we are checking to see if a variable contains a number, we don’t need to enclose the number
in quotes. However, if we are checking if a variable contains a string, remember to place quotes around the
string, so that the condition can be correctly evaluated. Also, indentation is vital here, so make sure you
indent correctly!

PRIMM TASKS
1: Predict

Code
What do you think the code will do?

Write the program’s output exactly as you think it will appear.


Predictions
>>>

2: Run

Now type in the code and run the program.


Show a screenshot of the actual output below.

Was it the same as your prediction? Yes No


Were there differences? If so, show or describe these below.

3: Investigate

What happens if the user is


asked to type in the numbers
1, 2 or 3, instead of Good,
OK and Bad?

What changes are needed to


get the code to run?
4: Modify

Modify the code so that it also Screenshot /Explanation


responds with a unique
message if the user types in the
word ‘terrible’.

Explain what your code is


doing.

5: Make
Create a program in python for each point below and add a screenshot of your code in the boxes provided:
1) Write a program that asks the user for a number and says if they have entered a zero or not.

2) Write a program that asks the user for a letter and says if the letter is an ‘a’ or not.

3) Write a program that asks the user for a day of the week and states what you will be doing on that day (e.g. if
‘Monday’, “…I’ve got school!”
4) Write a program that asks the user for a number from 1-3. If it is a 1, the program will add 10 to it. If it is a 2, the
program will multiply it by 10. If it is a 3, it will divide the number by 10.

5) Write a program that ask the user for a month. The program will return the days that the month contains (30 days
has September, April, June and November, all the rest have 31, except for February, which has 28, but 29 on a leap
year).

You might also like