ICT112 - 2023 - Module 3 - Workshop Instructions-1

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

ICT112 – Module 3 Workshop

Learning Objectives
In this Workshop you will:
1. Use variables to store values and update those values;
2. Draw simple shapes using Python 'Turtle Graphics' objects;
3. Use for loops to repeat a block of code a given number of times.

Task 1
Each week (week 1 to 8) you will be required to submit into Canvas your solution. Each
submission will be marked out of 5. Task 1 will be the total of your best 6 submissions (30 marks
possible).

Theme: Training Turtles


Apparently, turtles are a lot smarter and easier to train than
snakes! (http://www.anapsid.org/tamingvh.html )
So, in today's lab we are going to try our hands at training a Figure 1. http://en.wikipedia.org/wiki/Turtle
small turtle to walk in certain patterns,
and maybe even persuade him or her to create some pretty pictures in the sand.

Page 1
ICT112 – Module 3 Workshop

1. Training a Turtle
One of the fun things about the Python language is that one of its standard libraries is the 'Turtle
Graphics' module. Turtles are a great visual way of learning about Python objects and methods,
and practicing the problem-solving skills that are crucial to learning programming.
Note: you do not need to type in the 'comments' (everything from the '#' character to the end of the
line). Python ignores everything after a '#', so you can put comments in your code, but they just
for human readers, to explain what is going on, and why the code is written that way.

import turtle # Allows us to use turtles


alex = turtle.Turtle() # Create a turtle, assign to alex
wn = turtle.Screen() # create a window for our design
wn.exitonclick() # get rid of the screen. Last step

You should see an output window pop up at this point. Drag it over to the right side of your
screen so that it does not overlap your code window. Note that the screen stays around until you
click on it. This is because of the last command. Going forward make sure you put the additional
commands before the wn.exitonclick() line
Then type in the next few lines and watch what alex (the 'turtle') does.

import turtle # Allows us to use turtles


alex = turtle.Turtle() # Create a turtle, assign to alex
wn = turtle.Screen()

alex.forward(50)
alex.left(90)
alex.forward(30)

wn.exitonclick() # get rid of the screen. Last step

We now have a turtle object that we call 'alex' (short for 'alexander' or 'alexandra'? It is difficult to
know the sex of turtles! http://www.wikihow.com/Tell-if-a-Turtle-Is-Male-or-Female ). Then we
give 'alex' a few instructions on how to move, and as he/she moves along, a line is drawn in the
imaginary sand.
You can find more explanation of this program and more details about turtles in Chapter 4
('Python Turtle Graphics') of our online textbook:
Another useful source of reference documentation about turtles is the online Python
documentation:
 https://docs.python.org/3/library/turtle.html

Page 2
ICT112 – Module 3 Workshop

2. Some Simple Polygons


Here is a series of simple shapes to draw, to learn how to control the turtle and figure out how the
angles work. After you complete each task, add an explanatory comment line above your code to
explain what it does (for example: # draws a square). Then start drawing the next shape, by
copying and pasting any bits of code that you need to reuse.

Extend the above program so that it draws the whole rectangle, 50 pixels wide and 30 pixels high.

1. Modify the rectangle code so that it draws a square, 100 by 100 pixels.

2. Add some extra code to draw an equilateral triangle, with three sides, each 100 pixels long.

3. Also draw a hexagon that has six sides, each 100 pixels long. Hmm, this hexagon code is
getting to be a bit long and repetitive. Imagine how long our program would be if we
wanted to draw a shape with 20 sides!
a. Can you use a for loop to make your hexagon code shorter?
b. Hint: for side in range(6): ... (Make sure that all the code you want to repeat inside
the loop is indented by four spaces.)

4. Define a variable: size = 90. Then change your hexagon code so that it uses your size
variable for the length of each side. Rerun your hexagon code.

5. Change your size variable to be 180. Then rerun your hexagon code. Does it look larger?

Add comments at the top of your file with your name the date of this version and what it does.
Save your python in a file call “polygons.py” ready for submission.

Page 3
ICT112 – Module 3 Workshop

3. Making it colourful
Let's make our shapes picture look a bit more colourful. We can call:
alex.pencolor("red")
to set the colour of the pen to red - note the American spelling of 'color' in the method name. You
can find a list of the string colour names that Python knows about here: http://wiki.tcl.tk/37701.
Okay, now beautify your three shapes as follows:
1. Change your square so that it is filled in with a nice light colour.
 Hint: ask alex to begin_fill() before you start drawing the square,
 end_fill() after you finish drawing the square.
 The end_fill() method will paint the interior of the shape that you have just drawn.
 You can call alex.fillcolor("color name") to set the fill color any time before you call
end_fill().
2. Change your triangle so that it is also filled in, but choose a darker or complementary
colour that fits in nicely with the colour of your square.
3. Change your hexagon so that it is drawn with dark, bold lines, 5 pixels wide. It should not
be filled, just an outline shape. Choose a dark colour for the lines, so that the hexagon
creates a nice border for your picture.
Your final picture should look something like this (but with your colours):

Add comments at the top of your file with your name the date of this version and what it does.
Save your python in a file call “colours.py” ready for submission.

Page 4
ICT112 – Module 3 Workshop

4. Going a bit loopy


For this part of the lab, we are going to use Python for loops to draw some slightly more
interesting and complex pictures. We will start by drawing circles and some pictures like old-
fashioned spirographs (http://en.wikipedia.org/wiki/Spirograph), and end by plotting the path of a
drunk pirate.

Now, copy and paste the following example code into your program:
# a Python program to draw circles/spirals
alex.reset()
alex.speed(10)

for i in range(360):
alex.forward(1)
alex.left(1)

You will find that this draws a circle. But rather slowly, because it takes 360 tiny left turns to get
around the whole circle! Try reducing the 360 iterations to just 36, and increase both 1's to 10,
and it will draw more quickly, but still look quite close to a circle.
To make a more interesting picture, copy the code for your triangle and paste it inside this circle
loop (just after the alex.left(10) line). Make sure that your triangle code is indented the same
amount as the preceding lines, so that all the code inside the loop is at the same level. This is how
Python knows which lines of code are inside the loop. Then run your program again and see what
you see.
Add comments at the top of your file with your name the date of this version and what it does.
Save your python in a file call “spirograph.py” ready for submission.

Page 5
ICT112 – Module 3 Workshop

5. Spiralling Around (Extra)


Experiment with varying the length of the
forward(10) method call at the top of the loop.
Also try changing it to forward(10+i).
Then can you change your program so it draws a
spiral of triangles like the ones shown here?
Or get creative, and see if you can draw some more
interesting spiral shapes!

6. The Drunk Pirate (Extra)


A drunk pirate makes a random turn and then takes
100 steps forward, makes another random turn, takes
another 100 steps, turns another random amount, etc. A
social science student records the angle of each turn
before the next 100 steps are taken. Her
experimental data is:

Figure 2. http://www.blueinkalchemy.com/tag/pirates/

angles = [160, -43, 270, -97, -43, 200, -940, 17, -86]
(Positive angles are counter-clockwise.)
Write a turtle graphics program, which uses your turtle to draw the path taken by our drunken
friend. Make his path wide and red.
Hint: to loop through the angles, after you have added the above definition of the list of angles,
you can use: for a in angles:

Page 6
ICT112 – Module 3 Workshop

7. A random drunk pirate (Extra)


Instead of following the same (historical) path of the same drunk pirate every time your program
runs, can we model a drunk pirate who takes a different (and random) path each time? Yes,
Python has a ‘random’ module that makes it easy to do this. For example:

import random
for step in range(0,10):
angle = random.randint(-90,120)
print("Pirate turns by " + str(angle) + " degrees.")

Can you integrate this idea of 'random angles' into your program, to simulate a real drunk pirate?
Can you make him/her turn right as well as left randomly?

Task 1 Submission
1. Submit polygons.py (1 marks)

2. Submit colours.py (2 marks)

3. Submit spirograph.py (2 marks)

Page 7

You might also like