John Palmer Faculty Head: IT, Computing & Business – The Chase School, Malvern CAS Hub Leader: “Three Counties” Hub (Hereford, Worcs and Gloucs) CAS Master Teacher Regional Coordinator: Network of Excellence (Secondary) @johnny__palmer [email protected]
81
Embed
John Palmer - Worc Inputs/CAS... · •Plan simple algorithms using flowcharts ... use different types of data including Boolean, ... Equivalent pseudocode
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
John Palmer Faculty Head: IT, Computing & Business – The Chase School, Malvern
CAS Hub Leader: “Three Counties” Hub (Hereford, Worcs and Gloucs)
CAS Master Teacher
Regional Coordinator: Network of Excellence (Secondary)
• Looking to teach coding as part of Computing, (most probably
GCSE)
• Little / old coding experience (maybe in Python)
• Not sure what the exam boards require pupils to know
• This is a functional introduction to coding in Python (we can’t
cover everything in 2 hrs....)
• There will be follow – up for those interested in developing
their use of Python for GCSE
• NB - not just about learning the Python coding language, it’s
also about HOW you teach Python to children...
Why are you here?
Why Python?
• It’s cool
• It’s Open Source
• It’s a great introductory language
• It’s easy to learn – the students like it
• A “console” based language (not visual, but good for teaching the basics...)
• Loads of free learning resources / books
• The Computing curriculum is being “crowd-sourced”
• Supported by most (all) exam boards?
• Try not to worry to much about which language you are using, it’s not just about that after all....
• It is however an important choice....
• Many people are using Python successfully in the classroom for GCSE Computing
Python 3 vs Python 2
• Python 2 is the status quo, Python 3 is the present and future of the language
• There will be no new major releases for Python 2 beyond 2.7. “the 2.x line will only receive bugfixes, and new features are developed for 3.x only.” – python.org
• Graphical User Interfaces can already be created using Tkinter which already comes with Python 3. PyQt4 already supports Python 3.
Overview
Housekeeping
Introduction
Session 1
Session 2
Plenary
Session 3
Homework
Webinar
Questions
Feedback
Learning Outcomes:
By the end of this course you will be able to:
• Plan simple algorithms using flowcharts
and pseudocode
• Code simple algorithms using Python
• Understand and use the three basic
programming constructs used to control the flow
of a program
Introduction
Overview
Housekeeping
Introduction
Session 1
Session 2
Plenary
Session 3
Homework
Webinar
Questions
Feedback
• Would you expect 16 year-olds to pass GCSE Maths after 120 lessons?
• For the OCR GCSE Coding Based Controlled Assessment (and
tested in the exam):
(a) identify and use variables, operators, inputs, outputs and assignments
(b) understand and use the three basic programming constructs used to control
the flow of a program: Sequence; Conditionals; Iteration
(c) understand and use suitable loops including count and condition
controlled loops
(d) use different types of data including Boolean, string, integer and real
appropriately in solutions to problems
(e) understand and use basic string manipulation
(f) understand and use basic file handling operations: open, read, write and close
(g) define and use arrays as appropriate when solving problems.
Typical GCSE expectations
Important Newsflash
It’s less about being able to “code in
a certain language” and more about
being able to “think Computationally
and solve problems”
KS3 building to GCSE
• GCSE Computing courses require pupils to:
– Understand the operation of algorithms
– Design algorithms
– Produce algorithms to solve problems
• CAS curriculum for KS3:
– “A student should understand what an algorithm is,
and what algorithms can be used for”
Developing algorithms
• Pupils we teach to GCSE will need to develop
algorithms that use:
– Sequence
– Selection
– Iteration
• …maybe we should start teaching them this
earlier!
Sequence
• The starting point for all algorithms
• Series of instructions one after each other
Pick up cup
Drink coffee
Put down
Selection
• Useful algorithms will need to react to
conditions that occur
Is mug empty?
Drink coffee
Put down
Nibble biscuit
Iteration
• Repeat parts of an algorithm to approach the
desired outcome
Pick up mug
Is there coffee in mug?
Put down
Drink coffee
Planning using flowcharts
• Graphical representation of
algorithm sequence.
• Good flowchart design must:
– Begin and end with the same shape
– Connect all steps with a direction
arrow
– Be evenly spaced
– Have clear and concise descriptions.
Planning using pseudo code
• This is a list of instructions
outlining the operation of an
algorithm written so that a
human can understand them.
• Good pseudo code design must:
– Have line numbers
– Have clear descriptions without
being too complex
– Be indented to show similar
blocks of code
– List actions as capital letters.
What next?
• Look at algorithm designs that use sequence,
selection and iteration
• Produce code from these designs using
Python
Hints
• Never use the P – word – “coding” is better
• “Code cards” are good for teaching syntax
• Give students example code / code to alter?
• Look at “algorithms” using pseudo code /
flowcharts then produce code using Python
• They are children!
Is Python working?
1. Open up IDLE, (basic Python programming
environment)
2. Type these commands into the interactive shell
(answering where necessary)
Overview
Housekeeping
Introduction
Session 1
Session 2
Plenary
Session 3
Homework
Webinar
Questions
Feedback
• “The beginning is the most important part of the work.” Plato
Session 2: From algorithms to Python code
Describing the simple start
• If simple programs are easy to code why
bother with design?
Size of problem
Effort Hacker’s curve
Size of problem
Effort
Designer’s line
Flowcharts
• If simple programs are easy to code why bother with design?
Start End
Input Output
Process
Used to define the start and end points of the algorithm
Input – what the user enters into the program Output – what is produced by the computer, (on screen, printed, etc.)
Used to explain anything the computer needs to work out, (eg 2 + 2)
Decision
Used to create different paths
through the algorithm
Algorithms that use sequences
• Consider the program we made as a flowchart:
Start
OUTPUT “What is
your name?"
INPUT name
OUTPUT “Hello” +
name
End
Overkill when you know what you are doing but may help pupils visualise what is happening.
Equivalent pseudocode
• A detailed flowchart allows you to create quick pseudocode:
Start
OUTPUT “What is
your name?"
INPUT name
OUTPUT “Hello” +
name
End
1. START 2. OUTPUT “What is your name?” 3. INPUT name 4. OUTPUT “Hello” + name 5. END
Python parts
• Python programs can be constructed quickly and
executed through IDLE, (Interactive DeveLopment
Environment)
Single command typed and executed through the interactive shell
Text editor
• Programs are usually more than one line so use the
file editor to write your programs, (when executed
(F5) the output appears in the interactive shell)
Text editor
• When writing a program it is good practice to
describe parts that aren’t obvious, (using comments)
Python Data Types
Data Type Python Data Type
Example
Integer int() 5
Real float() 5.5
String str() 'Hello'
Boolean bool() True
Python Operators
Operator Python Code Answer
Addition Subtraction
Multiplication Division
3+5 5-1 3*3 5/2
8 4 9
2.5
Exponent (repeated multiplication)
2**3 8
Modulo (remainder after division)
12%5 2
Equal Not Equal
10==9 'Jaz'!=’Fred'
False True
Greater than Less than
'z'>'a' 5<3
True False
+ Assignment, Input and Output in Python
• Some languages require that variables be declared (and data type specified) before they are used to store values. Python does not require the declaration of variables before use.
• For example the code required to create a variable named count of data type integer storing (assigning) the value 5 is: count = 5
• If you wanted to store this as a real/float: count = 5.0
+ Assignment, Input and Output in Python
• Python makes it easy to convert valid data between types.
• A valid string can be converted into real/float, integer or boolean: float('3.5') int('55') bool('True')
• A real/float can be converted into an integer (it will be rounded down): int(3.8)
• An integer can be converted into a real/float: float(6)
Your turn
Now try making some sequential
programs using Python.
Your turn
# A program for greeting people
name = input ("What is your name? ")
print ("Hello, " + name + "! How are you?")
print ("My name is" , name)
1) Copy this into IDLE, and Save the file into your Python Folder with the
name "greeting.py". Press F5 to run.
2) Write a program that asks two people for their names; stores the names in
variables called name1 and name2; says hello to both of them.
Your turn
# Numbers code
a = 3
b = 5
c = 7
print ("These are the values:", a, b, c)
print ("Increment", a, "by one: ")
a = a +1
print (a)
print ("The sum of", a, "and", b, "is")
d = a + b
print (d)
number = int(input("Input a number "))
print ("Number is ", number)
number2 = number * 2
print ("Number times two is", number2)
Copy this into IDLE, and Save the file into your Python Folder with the name
"numbers.py". Press F5 to run.
Execute this algorithm!
Drink coffee
Has 5 minutes elapsed?
Start session 2
Eat biscuits
Algorithms that use conditions
• Programs rarely run in order, needing to
respond to events and change the order
they run in
• This involves conditional constructs, (parts of
the coding language we can use)
IF statements
• Check is a condition is TRUE and execute a related statement:
IF condition is true THEN execute this statement END IF
IF score > 50 THEN OUTPUT “You passed!” END IF
Is score > 50?
Output “You
passed!”
Yes
No
IF…ELSE statements
• ELSE can be added to IF to offer an alternative if the first condition isn’t met:
IF condition is true THEN execute this statement ELSE execute this statement END IF
IF score > 50 THEN OUTPUT “You passed!” ELSE OUTPUT “You failed!” END IF
Is score > 50?
Output “You
passed!”
Yes
No
Output “You
failed!”
ELSE IF statements
• Can add as many tests that you want:
IF condition is true THEN execute this statement ELSE IF condition is true THEN execute this statement ELSE execute this statement END IF
IF score > 50 THEN OUTPUT “You passed!” ELSE IF score > 0 THEN OUTPUT “You failed!” ELSE OUTPUT “Zero scored!” END IF
Is score > 50?
Output “You
passed!”
Yes
No
Is score > 0?
Output “You
failed!”
Yes
Output “Zero
scored!”
No
Using conditional constructs in Python
• Python works on white space and indents to separate IF statements into blocks:
Nesting IFs
• The advantage of the block method is that the programmer has to think about making the code easy to read:
A very important point!
• Indentation is key! It determines where the start and end is of different blocks of code.
• SyntaxError: unexpected indent indicates a problem with your indentation.
• And don’t forget colons in IF / WHILE statements
Your turn
Now try making some conditional
programs using Python.
Your turn
# works out whether a number is positive,
# negative or zero
num = int(input("Enter a whole number: "))
if num < 0:
print("The number is negative")
elif num > 0:
print("The number is positive")
else:
print("The number is zero")
Copy this into IDLE, and Save the file into your Python Folder with the name "numchecker.py". Press F5 to run
Your turn
1) Write code from this flowchart 2) Write code that asks for the user’s age to be typed in at the keyboard, then displays a message that states whether or not the user is old enough to drive. 3) Write code that asks for two numbers to be typed in at the keyboard, then display the larger of the two numbers.
Overview
Housekeeping
Introduction
Session 1
Session 2
Plenary
Session 3
Homework
Webinar
Questions
Feedback
• Plan algorithms using flowcharts
and pseudocode
• Implement simple algorithms using Python
• Understand and use some basic
programming constructs used to control the
flow of a program
Plenary
Interactive websites for learning Python
• Computer Science Circles – University of Waterloo, Canada http://cscircles.cemc.uwaterloo.ca/
• How to think like a Computer Scientist - Interactive Edition http://interactivepython.org/courselib/static/thinkcspy/index.html
• Problem solving with Algorithms and Data Structures using Python http://interactivepython.org/courselib/static/pythonds/index.html
• Pygame – a set of Python modules designed for writing games. http://www.pygame.org/news.html
d) There's some cool stuff on Python here on Khan Academy http://www.khanacademy.org/science/computer-science
e) How to Think Like a Computer Scientist is pretty good too, at http://interactivepython.org/courselib/static/thinkcspy/index.html#learning-with-python-interactive-edition-using-python-3-x
f) Especially go and look at the stuff on FOR loops and WHILE loops on
Khan Academy
…and relax!
LUNCH
Overview
Housekeeping
Introduction
Session 1
Session 2
Plenary
Session 3
Homework
Webinar
Questions
Feedback
• “Over and over and over and over and over; Like a monkey with a miniature cymbal” Hot Chip – Over and over
Session 2: Iterative programs
Algorithms that use iteration
• Programs often repeat themselves
• A loop can be programmed to run a given
number (count-controlled)
• Or a loop can continue until a certain finishing
state has been achieved (condition-controlled)
Loop comparison for plate of 3 biscuits
Eat biscuit
For biscuit 1 to 3
Next biscuit
Start on sandwiches
Eat biscuit
Are there biscuits on plate?
Next biscuit
Start on sandwiches
Count-controlled loops
• A loop that runs a specific number of times:
FOR var=start num TO end num execute this statement END FOR
FOR i = 1 TO 10 OUTPUT i END FOR
Is i <= 10?
Output i
Yes
No
i = i + 1
i = 1
Python FOR construct
• Again Python uses white space and indents to separate FOR statements into blocks:
Python FOR construct
• Note that Python needs to be told what to count to and how to count:
FOR i = 1 TO 10 OUTPUT i END FOR
for i in range(1,11): print(i)
FOR i = 10 TO 1 OUTPUT i END FOR
for i in range(10,0,-1): print(i)
Condition-controlled loops - WHILE
• Loop that runs until condition is met, (and may never run):
WHILE condition is true execute this statement END WHILE
WHILE x < 10 OUTPUT x x = x + 1 END WHILE
Is x < 10?
Output x
Yes
No
x = x + 1
Condition-controlled loops - REPEAT
• Loop that will run at least once:
REPEAT execute this statement UNTIL condition is true
REPEAT OUTPUT x x = x - 1 UNTIL x <= 0
Is x <= 0?
Output x
No
x = x - 1
Python WHILE construct
• Python uses white space and indents to separate WHILE statements into blocks:
Python REPEAT construct
• Python doesn’t have a specific REPEAT construct so we have to adapt a WHILE loop:
Your turn
Now try making some simple
iterative programs using Python.
Your turn
1) Use a While Loop to write code for this flowchart
2) Times tables Use a For loop to write Python code to print a multiplication table (a times table). At the start it should as the user which number they want to see the times table for by asking “Which times table would you like?”
Hints: you will need to use 2 variables and one of these will be the user input (the times table wanted). Example output: Which times table would you like? 5 Here’s your table: 5 x 1 = 5……..
Try this.....
What does this do? import random n = random.randint(1,100) print (n) 1) Dice Rolling
Write a program that simulates (acts like) rolling a die. The program should ask the user if they want to roll the dice, while they say yes it should tell them the number they have rolled (“You have rolled a “). It should ask them again after each roll. If they say no, the program should say ‘Goodbye’
2) Guess the number game.
Create a game where the computer generates a random integer between 1 and 100 and the player has to try and guess it in as few attempts as possible. Each time the player enters a guess the computer tells the player whether the guess is too high, too low or correct! Once the player guesses the number the game is over.
“It’s no good joyfully writing a curriculum if no
one can teach it”
Adrian Mee, IoE, University of London
I have a piece of paper that is more intelligent than anyone
in this room …
Maybe things we don’t have time for...
Playing Noughts & Crosses Perfectly
Move 1: Go in a corner.
Move 2: If the other player did not go there then go in the opposite corner to move 1.
Otherwise go in a free corner.
Move 3: If there are 2 Xs and a space in a line (in any order) then go in the space.
Otherwise if there are 2 Os and a space in a line then go in that space.
Otherwise go in a free corner.
Move 4: If there are 2 Xs and a space in a line (in any order) then go in the space.
Otherwise if there are 2 Os and a space in a line then go in that space.
Otherwise go in a free corner.
Move 5: Go in the free space.
Explaining algorithms
• Having played OXO you are in a position to: – Explain what an algorithm is
– What a program is
– Have a discussion of whether computers are intelligent if they are just following rules and even the philosophy of mind!
• Students can try to write a similar perfect set of instructions for playing second
• Programming without letting language syntax get in the way!
Other good algorithms
• Making sandwiches
• Changing tyres
• Painting finger nails
• Tying shoe laces
• Playing Black Jack
Other teaching ideas....
• Step away from the computer
• Students organise “code cards” into the correct order (hopefully!) - useful for teaching syntax and for asking “what changes if” type questions..
A LOGO class activity, works well in other languages too..
NB LOGO is highly recommended, esp at KS3. It is a powerful, Lisp-like language that was explicitly designed to be easy for students to learn. It can be used to
teach most computer science concepts!
A card activity introducing Assembly Language coding.....
Scratch “Jigsaws”
Files to edit / add to... NOT code that is wrong that
they need to correct...
Homework.....
Blackjack
• Set up variables for the player score, the dealer score • Deal two cards to each (use a random number) • Make a variable called end_the_game and set it equal to False • Make a loop that only runs while end_the_game is False (i.e. the game has not ended)
• Inside the loop, get input from the player to ask what he wants to do
• If his input was “stick” • print out his total and the dealer’s total • check who has won and print it • end the game
• If his input was “hit”: • draw him a new card and add it to his total • check if he has bust
• if so the game ends and he loses, if not print his new total • if he didn’t bust,
• if the dealer draws (only when he is on <17) • Give him a new card
• if the dealer is bust • the game ends and the player wins
Overview
Housekeeping
Introduction
Session 1
Session 2
Plenary
Session 3
Homework
Webinar
Questions
Feedback
Homework #2
Overview
Housekeeping
Introduction
Session 1
Session 2
Plenary
Session 3
Homework
Webinar
Questions
Feedback
Homework #3 – A453 Specimen Task
It would be excellent practice to complete this task. In order to complete it, you will need to independently investigate “lists “ and “string handling”. Design, code test and evaluate a system to accept and test a password for certain characteristics. It should be at least 6, and no more than 12 characters long • The system must indicate that the password has failed and why, asking the user to re • enter their choice until a successful password is entered. • A message to indicate that the password is acceptable must be displayed. • Password strength can be assessed against simple criteria to assess its suitability; for • example a password system using only upper and lower case alphabetical characters
and numeric characters could assess the password strength as: • WEAK if only one type used, eg all lower case or all numeric • MEDIUM if two types are used • STRONG if all three types are used.
For example hilltop, 123471324, HAHGFD are all WEAK, catman3 and 123456t are MEDIUM and RTH34gd is STRONG A message to indicate the password strength should be displayed after an acceptable password is chosen.
Overview
Housekeeping
Introduction
Session 1
Session 2
Plenary
Session 3
Homework
Webinar
Questions
Feedback
• Plan algorithms using flowcharts
and pseudocode
• Implement algorithms using Python
• Understand and use the three basic
programming constructs used to
control the flow of a program
Plenary
More stuff.....
• That we probably haven’t had time to cover... for GCSE you also need to know about “lists” and “file handling”
• Running a session on all this at Pate’s next week where we will also go through the A453 practical tasks – there will be another session in the Autumn term
More stuff....
• All the resources from this session I will make available on CAS Online and link to from the symposium website