Conditions. Objectives Understanding what altering the flow of control does on programs and being...

23
Conditions

Transcript of Conditions. Objectives Understanding what altering the flow of control does on programs and being...

Page 1: Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.

Conditions

Page 2: Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.

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

Page 3: Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.

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 <condition> thenoperation(s) for <then-part>

elseoperation(s) for <else-part>

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 <then-part>

3. If it is false then perform the operation(s) in the <else-part>

Conditions 3 of 22

Page 4: Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.

Conditions: Review Problem: Work out whether the student has passed or failed

Startget markif mark >= 50

result = “Pass”else

result = “Fail”end-ifdisplay result

StopConditions 4 of 22

Page 5: Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.

Conditions

Conditions: Review There can be more than one expression

in the <then> or <else> parts. E.g.

StartGet markif mark >= 50 thenresult = “Pass”message = “Well done!”elseresult = “Fail”message = “See me!”end-ifdisplay result display message

Stop 5 of 22

Page 6: Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.

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”

Conditions 6 of 22

Page 7: Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.

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

Operator Meaning Example Evaluation

== is equal to 7 == 8 False

!= is not equal to 7 != 8 True< is less than 5 < 10 True> is greater than 10 > 9 True<= Is less than or equal to 10 <= 10 True>= is greater than or equal to 11 >= 5 True

7 of 22

Page 8: Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.

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

Page 9: Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.

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

Conditions 9 of 22

Page 10: Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.

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!Conditions 10 of 22

Page 11: Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.

Conditions An if-statement can also cater for alternate values

Try thisNotice the indentation

Page 12: Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.

Conditions It is also possible to have multiple conditions.

Start:Get markif 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…Conditions 12 of 22

Page 13: Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.

Condition Flowchart

Page 14: Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.

If…elif…else In Python, elif is short for “else if. It introduces alternate

conditions

Try this

Conditions 14 of 22

Page 15: Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.

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”

Conditions 15 of 22

1. Write pseudo code or draw a flowchart2. Test it3. Write the Python Program4. Test it

Page 16: Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.

Guessing My Number: Solution

Page 17: Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.

Guessing My Number: Solution

Conditions 17 of 22

Page 18: Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.

Your Turn: Calculate DiscountIn 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…Conditions 18 of 22

Page 19: Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.

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

Page 20: Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.

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

Page 21: Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.

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.

Page 22: Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.

Magic 8 Ball Phase 2You can now complete phase 2 of the Magic 8 Ball Exercise.

Magic8BallExercise.pdf

Page 23: Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.

HomeworkComplete the following exercises for homework. Rock, paper scissors programming challengeDue 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

Conditions 23 of 22