python-part-4-notes
python-part-4-notes
Contents
1.
Challenge
6
and
battleships
..........................................................................................
1
2.
Recap
functions
............................................................................................................
1
3.
Draw
shape
function
.....................................................................................................
2
4.
Solutions
to
Section
3
exercises
....................................................................................
4
5.
For
loops
.......................................................................................................................
5
6.
Resources
......................................................................................................................
5
2. Recap
functions
Reusing
code,
putting
a
unit
of
work
into
a
separate
function
(or
procedure)
and
using
it
again
wherever
necessary.
Also
like
sub-‐routines,
breaking
up
a
program
into
smaller
component
parts.
Do
3.3a
from
book
(p.
19)
with
class,
save
to
file
firstfunction.py.
# main program
counter = 0
while counter < 21:
nsquared_plus_n(counter ** 2, counter)
counter = counter + 1
window = turtle.Screen()
window.exitonclick()
Modify
as
follows:
import turtle
# function definition
def draw_square():
sidesToDraw = 4
angle = 360 / sidesToDraw
# main program
turtle.color('green')
draw_square()
window = turtle.Screen()
window.exitonclick()
Class
exercises
• create
a
draw_triangle
function
and
draw
a
red
triangle
• create
a
draw_hexagon
function
and
draw
a
blue
hexagon
turtle.right(angle)
sidesToDraw = sidesToDraw – 1
def draw_triangle():
sidesToDraw = 3
angle = 360 / sidesToDraw
def draw_hexagon():
sidesToDraw = 6
angle = 360 / sidesToDraw
# main program
turtle.color('green')
draw_square()
turtle.color('red')
draw_triangle()
turtle.color('blue')
draw_hexagon()
window = turtle.Screen()
window.exitonclick()
Question:
what
is
the
problem
with
the
above
code?
Answer:
code
duplication
–
the
only
difference
between
draw_square,
draw_triangle
and
draw_hexagon
is
the
value
of
sidesToDraw,
which
is
fixed/hard-‐coded
in
each
function.
Together
with
class
develop
a
draw_shape
function
that
can
draw
any
equilateral
shape.
For draw_shape
version
1
(parameterised
with
sidesToDraw),
take
the
draw_hexagon
function,
rename
it
to
draw_shape,
parameterise
with
sidesToDraw,
and
delete
the
line
that
gives
sidesToDraw
the
value
6.
def draw_shape(sidesToDraw):
angle = 360 / sidesToDraw
draw_shape
is
a
function
that
will
draw
any
regular
polygon
(equilateral
and
equiangular)
with
sides
of
length
100.
To
draw
different
shapes
just
call
the
function
with
different
values
for
the
sidesToDraw,
e.g:
# green square
turtle.color('green')
draw_shape(4)
# red triangle
turtle.color('red')
draw_shape(3)
# blue pentagon
turtle.color('blue')
draw_shape(5)
Question:
what
happens
when
the
number
of
sides
to
draw
is
less
than
3?
Question:
what
other
fixed
values
in
the
function
could
be
passed
as
parameters?
Class
exercises
• Change
the
draw_shape
function
to
print
a
message
to
say
that
sidesToDraw
is
too
small
for
a
suitable
value
of
sidesToDraw
and
only
draw
a
shape
if
sidesToDraw
is
greater
than
the
value.
• Consider
which
other
fixed
value
in
the
function
could
be
parameterised
to
allow
us
to
control
the
size
of
the
shape.
Change
the
function
accordingly.
# blue square
draw_shape(100, 4)
# red triangle
draw_shape(100, 3)
# green pentagon
draw_shape(100, 5)
Demonstrate
changing
lineLength.
Question:
what
are
the
differences
between
draw_square
and
draw_shape
functions?
Class
exercises
• Modify
myshapes.py
to
provide
functions
to
draw
a
square,
triangle
and
a
pentagon
using
the
draw_shape
function
functions.
5. For
loops
We
often
know
how
many
times
we
want
to
iterate
(as
in
the
draw
shape
example).
We
can
use
a
for
loop
for
this.
Work
through
Section
3.2b
on
p.16
of
Mark
Clarkson's
book.
On
whiteboard:
general
form
of
for
loop
for i in range([start,] stop [,step]):
# do something
Starting
with
a
value
for
variable
i
of
start,
iterate
through
the
loop
(executing
any
code
in
the
block
on
each
iteration)
until
ireaches
the
value
stop.
At
the
end
of
each
iteration,
increment
the
value
of
i
by
step.
The
default
value
for
start
is
0.
The
default
value
for
step
is
1.
Class
exercises
1. In
a
program
file,
do
3.2c
loop
challenges
on
p.16
of
Mark
Clarkson's
book.
o For
challenge
3
of
3.2c,
write
a
function
to
calculate
the
times
table
with
given
input.
2. Do
3.2f
loop
challenge
on
p.18
of
Mark
Clarkson's
book,
writing
a
function
to
calculate
the
factorial
6. Resources
• Other
material
from
Newcastle
University
Introduction
to
Python
CPD:
http://www.ncl.ac.uk/computing/outreach/resources/programming/python/intro2p
ython
• Mark
Clarkson's
Introduction
to
Python
resources
including
textbook,
workbooks,
example
code
and
GCSE
controlled
assessment:
http://www.ncl.ac.uk/computing/outreach/resources/protected/mwclarkson-‐
resources.zip
• Other
Python
resources:
http://www.ncl.ac.uk/computing/outreach/resources/programming/python/