Python Lesson 5

advertisement
PROGRAMMING
In
Lesson 5
RECAP
 Complete the starter activity
OBJECTIVES
 Understand how to use looping statement (while)
 Understand to use the break statement
 Understand the development process of flow charts to comments to
code
OUTCOMES
 All-Level 4
written code using while - with help
developed programs using pseudo code
 Most-Level 5
Be able to write a these with little help.
 Some-Level 6
Independently write these and adapt them and
complete the extension tasks.
LOOPS
 You might remember this code from your truth table of last lesson. It is
part of the function that got the user input.
 It uses a while statement to keep asking the user to input a value until it
is one of the required values.
WHILE STATEMENT
Don’t forget the full colon
while expression :
statement 1 Statements are executed
Notice the
indentation – statement 2 while the expression is true
this defines the
statements that statement 3
are in the while
statement
WRITE A LOOPY PROGRAM – PSEUDO CODE
start
Ask user for a number
Write a program that asks the user for a
number and counts down to 1 displaying each
number as it goes.
Convert
to integer
• Write
the
pseudo code for the program.
a=int(a) – converts number a to an integer
You havecomments
the rest of the
knowledge.
• We will then write the program
from
this pseudo
code Is
No
number
>0
end
• We will then fill in the code for each comment.
Yes
Display number
Subtract one
Extension:
Get you program to calculate the factorial of the
number.
Hint: factorial of 4 is 4 * 3 * 2 * 1.
WRITE A LOOPY PROGRAM - COMMENTS
start
Ask user for a number
Convert to integer
Is
number
>0
Yes
Display number
Subtract one
No
end
WRITE A LOOPY PROGRAM - CODE
Extension:
Get you program to calculate the factorial of the number.
Hint: factorial of 4 is 4 * 3 * 2 * 1.
CLASS DISCUSSION
 With your classmate you have 2 mins to discuss…
 What was most difficult in the program
 What was most fun
 What was the boring part
WHILE STATEMENT BREAK
while expression :
Notice the
statement 1 Statements are executed
indentation –
while the expression is true
statement
2
this defines
which
statements are
in the while
statement
break
This command quits from the
while statement – even if the
expression is still true.
GUESS THE NUMBER – VERSION 2
 Remember this – guessing a random number….
GUESS THE NUMBER – VERSION 2
 I want you to make it even better
 Give the user 6 chances to guess the number - use while statement
 Let them know if their guess is too high - if statement
 Let them know if their guess is too low - if statement
WHAT DO WE NEED TO CHANGE
Set guesses to zero
Guesses
>6
No
Yes
Exit from while loop
You have previously written most of this
Add 1 to guesses
program.
User has a guess
All you need to do is writeYesthe new
Guess <
Tell user too low
number
section
No
Guess >
number
Yes
Tell user too high
No
Guess =
number
No
Yes
Exit from while loop
Set guesses to zero
Guess
es > 6
Yes
Exit from while
loop
No
Add 1 to guesses
User has a guess
Guess
<
numbe
r
No
Guess
>
numbe
r
No
Guess
=
numbe
No r
Yes
Tell user too low
Yes
Yes
Tell user too high
Exit from while
loop
WRITE THE CODE
EXTENSIONS
 Make the random number range from 1 to 30
 Let them know the random number if they fail to guess it.
PLENARY
 Complete the Lesson 5 plenary Quiz
Download