Download presentation
Presentation is loading. Please wait.
Published byNancy Webster Modified over 9 years ago
1
Conditions
2
Objectives Understanding what altering the flow of control does on programs and being able to apply thee to design code Look at why indentation is important and what syntax errors you will get if you don’t use them Use the if, elif and else conditions on different programming scenarios Look at what trace tables are and why testing is important
3
Conditions If all a computer could do was carry out a single series of steps then it wouldn’t be much use. Being able to choose between mutually exclusive alternatives is known as altering the flow of control. if then operation(s) for else operation(s) for Conditions are the way we make a choice between two or more alternatives. A condition must be a Boolean expression 1. Evaluate the condition getting True or False 2. If it is true then perform the operation(s) in the 3. If it is false then perform the operation(s) in the Conditions3 of 22
4
Conditions: Review Problem: Work out whether the student has passed or failed Start get mark if mark >= 50 result = “Pass” else result = “Fail” end-if display result Stop Conditions4 of 22
5
Conditions: Review There can be more than one expression in the or parts. E.g. Start Get mark if mark >= 50 then result = “Pass” message = “Well done!” else result = “Fail” message = “See me!” end-if display result display message Stop Conditions5 of 22
6
Conditions We use “if”statements in Python for conditions Try this The condition, here guess == secret, must evaluate to True or False An “if” statement always begin with the keyword, if This is a conditional operator meaning ‘is equal to’ The colon is mandatory here This is indent (tab). It is automatically inserted automatically after a line ending in a colon, it is mandatory. These indented lines are run if the condition is true This line is always run, regardless of the value of “guess” Conditions6 of 22
7
Conditional Operators We have just met our first conditional operator: == Do not mix up the assignment operator = with the conditional operator == Try 7==8 and 7=8 in IDLE Notice that expressions using these conditional operators evaluate to True or False Try them in IDLE’s interactive mode OperatorMeaningExampleEvaluation ==is equal to7 == 8False !=is not equal to7 != 8True <is less than5 < 10True >is greater than10 > 9True <=Is less than or equal to10 <= 10True >=is greater than or equal to11 >= 5True 7 of 22
8
Indentation Python uses a colon and indentation to indicate what is called a code block. In the example below, there are three lines in the conditional code block which are executed if answer == “N” Try this This is the code block for the answer == “N” condition. These lines are only executed if answer == “N” This statement is executed regardless of the value of answer
9
Indentation Indentation (using tabs) is very important in Python It is optional in most languages but mandatory in Python Failing to indent will result in a syntax error Conditions9 of 22
10
Indentation What’s wrong with this? This line must be indented, it is part of the conditional code block. As it stands, the score would be incremented even if they got the answer wrong! Conditions10 of 22
11
Conditions An if-statement can also cater for alternate values Try this Notice the indentation
12
Conditions It is also possible to have multiple conditions. Start: Get mark if mark < 50 then grade = “Fail” else if mark < 60 then grade = “Pass” else if mark < 70 then grade = “Merit” else grade = “Distinction” display grade Stop Note that these conditions are mutually exclusive… Conditions12 of 22
13
Condition Flowchart
14
If…elif…else In Python, elif is short for “else if. It introduces alternate conditions Try this Conditions14 of 22
15
Your Turn: Guessing My Number Write a number guessing game Create a variable, secret, containing a number from 1 to 20 Ask the user to guess the number If they get it right say, “Yes, that’s right” If their guess is too low say, “No, that’s too low” If their guess is too high say, “No, that’s too high” Conditions15 of 22 1. Write pseudo code or draw a flowchart 2. Test it 3. Write the Python Program 4. Test it
16
Guessing My Number: Solution
17
Conditions17 of 22
18
Your Turn: Calculate Discount In a shop, customers get a discount depending on how much they spend If they spend less than £50, there is no discount Otherwise, if they spend less than £100, the discount is 5% Otherwise, if they spend less than £200, the discount is 7% Otherwise, the discount is 10% Write a program to ask the user to enter how much the customer has spent display the discount due in £’s to the customer Pseudo code or flowchart and testing first… Conditions18 of 22
19
The Magic 8 Ball – Phase 1 Magic8BallExercise.pdf Should I go to the cinema tonight? Answer is hazy, ask later! Signs point to yes! Will I win the lottery this week? Look in Magic8BallExercise.pdf on the VLE for instructions
20
Using Modules So far we have only used in-built functions and keywords such as print(), input() and if Python comes with many other different modules of pre- written code that we might not use so often We can import these into our programs if we want to use them We are going to use a module call “random” to help us to generate a random number
21
Importing a Module Add this line to the top of your program to import the module Now you can use the random.randint() function to get a random number Usage: random.randint(a,b) This function returns a random number between “a” and “b” (inclusive). E.g.
22
Magic 8 Ball Phase 2 You can now complete phase 2 of the Magic 8 Ball Exercise. Magic8BallExercise.pdf
23
Homework Complete the following exercises for homework. Rock, paper scissors programming challenge Due Monday after half term (03.11.14) Printed copies of code in folders… Write Python programs for the pseudo code on the following slides in “Flowchart and Pseudo Code” presentation (week 1). Slide 9 Slide 13 Slide 17 Conditions23 of 22
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.