0% found this document useful (0 votes)
2 views

python-part-4-notes

The document provides notes for a programming session focused on Python, covering topics such as functions, loops, and drawing shapes using the turtle graphics library. It includes challenges like creating a number guessing game and drawing various geometric shapes through parameterized functions. Additionally, it emphasizes code reuse and the importance of avoiding duplication in function definitions.

Uploaded by

v5jyw66v97
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

python-part-4-notes

The document provides notes for a programming session focused on Python, covering topics such as functions, loops, and drawing shapes using the turtle graphics library. It includes challenges like creating a number guessing game and drawing various geometric shapes through parameterized functions. Additionally, it emphasizes code reuse and the importance of avoiding duplication in function definitions.

Uploaded by

v5jyw66v97
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

 

   

Introduction  to  Programming  with  Python  –  Session  4  Notes  


Nick  Cook,  School  of  Computing  Science,  Newcastle  University    

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  

1. Challenge  16  and  battleships  


Work  through  Challenge  16  from  Python  Programming  challenges:  
Write  a  program  where  the  computer  thinks  of  a  number  between  1  and  100  (i.e.  
picks  a  number  at  random).  The  program  then  asks  the  user  to  guess  what  
number  it  is  thinking  of.  If  the  user  guesses  correctly,  the  program  should  say  well  
done  and  say  how  many  guesses  it  took  the  user.  If  the  user  guesses  incorrectly,  it  
should  say  whether  the  number  the  computer  is  thinking  of  is  higher  or  lower  
than  the  user's  guess,  and  ask  the  user  to  guess  again.  
Then  play  the  first  two  battleships  from  the  CS-­‐Unplugged  searching  algorithms  
activity  <  http://csunplugged.org/searching-­‐algorithms>.    

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.  

Function  with  more  than  one  parameter  


Do  following  nsquared_plus_n  function  with  class,  save  to  file  nsquaredplusn.py  
# function definition
def nsquared_plus_n(nsquared, n):
print(nsquared, '+',n,'=',nsquared + n)

# main program
counter = 0
while counter < 21:
nsquared_plus_n(counter ** 2, counter)
counter = counter + 1

©  Newcastle  University,  2013     1  


   

Turtle  draw  a  square  function  


Open  myshapes.py  from  Part  3  and  do  draw_square  function  with  class:  
Delete  all  code  but  version  3,  so  left  with:  
import turtle
turtle.color('green')
sidesToDraw = 4
angle = 360 / sidesToDraw

while sidesToDraw > 0:


turtle.forward(100)
turtle.right(angle)
sidesToDraw = sidesToDraw – 1

window = turtle.Screen()
window.exitonclick()
Modify  as  follows:  
import turtle
# function definition
def draw_square():
sidesToDraw = 4
angle = 360 / sidesToDraw

while sidesToDraw > 0:


turtle.forward(100)
turtle.right(angle)
sidesToDraw = sidesToDraw - 1

# 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  

3. Draw  shape  function  


The  myshapes.py  program  from  Section  2  should  be:  
import turtle
# function definitions
def draw_square():
sidesToDraw = 4
angle = 360 / sidesToDraw

while (sidesToDraw > 0):


turtle.forward(100)

©  Newcastle  University,  2013     2  


   

turtle.right(angle)
sidesToDraw = sidesToDraw – 1

def draw_triangle():
sidesToDraw = 3
angle = 360 / sidesToDraw

while (sidesToDraw > 0):


turtle.forward(100)
turtle.right(angle)
sidesToDraw = sidesToDraw - 1

def draw_hexagon():
sidesToDraw = 6
angle = 360 / sidesToDraw

while (sidesToDraw > 0):


turtle.forward(100)
turtle.right(angle)
sidesToDraw = sidesToDraw - 1

# 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

while (sidesToDraw > 0):


turtle.forward(100)
turtle.right(angle)
sidesToDraw = sidesToDraw – 1

©  Newcastle  University,  2013     3  


   

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.  

4. Solutions  to  Section  3  exercises  


Work  through  corrections  with  class.  Demonstrate  original  draw_shape  with  values  
of  2,  1,  0  for  sidesToDraw
Question:  how  do  we  correct  the  function?  
def draw_shape(sidesToDraw):
if sidesToDraw > 2:
angle = 360 / sidesToDraw

while sidesToDraw > 0:


turtle.forward(100)
turtle.right(angle)
sidesToDraw = sidesToDraw – 1
Question:  which  other  fixed  value  in  the  function  can  be  parameterised?  
• The  length  of  the  line:  
def draw_shape(lineLength, sidesToDraw):
if sidesToDraw > 2 and lineLength > 0:
angle = 360 / sidesToDraw

while sidesToDraw > 0:


turtle.forward(lineLength)
turtle.right(angle)
sidesToDraw = sidesToDraw – 1

©  Newcastle  University,  2013     4  


   

# 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/  

©  Newcastle  University,  2013     5  

You might also like