PDF

Python
1
TurtlePower
AllCodeClubsmustberegistered.Registeredclubsappearonthemapat
codeclubworld.org-ifyourclubisnotonthemapthenvisitjumpto.cc/18CpLPyto
findoutwhattodo.
Introduction:
Inthisproject,you’lllearnhowtousea‘turtle’todrawawesomeshapes
andpatterns.
ActivityChecklist
FollowtheseINSTRUCTIONSonebyone
TestyourProject
ClickonthegreenflagtoTESTyourcode
SaveyourProject
MakesuretoSAVEyourworknow
1
TheseprojectsareforuseoutsidetheUKonly.Moreinformationisavailableonourwebsiteatwww.codeclubworld.org.
ThiscourseworkisdevelopedintheopenonGitHub,atwww.github.com/CodeClub.Comeandjoinus!
Step1:Hello,turtle!
We’regoingtohavesomefunprogrammingturtles.Aturtleisatinyrobot
thatdrawsonyourscreen,andcanbecontrolledusingPythoncommands.
ActivityChecklist
1. Let’smakeaturtlemovearoundthescreen,byrunningthis
shortPythonprogram:
fromturtleimport*
shape("turtle")
speed(5)
forward(100)
right(90)
forward(100)
done()
2. Theturtlehasapenattached,anddrawsalineasitmoves
aroundthescreen.Here’swhattheprogramdoes:
fromturtleimport*
tellsPythonthatyou
wanttousetheturtlelibrary,acollectionof
codeyoucanusetodrawonthescreen.The
*
means‘importeverything’.
2
TheseprojectsareforuseoutsidetheUKonly.Moreinformationisavailableonourwebsiteatwww.codeclubworld.org.
ThiscourseworkisdevelopedintheopenonGitHub,atwww.github.com/CodeClub.Comeandjoinus!
shape("turtle")
makesthedrawingrobotlook
likeaturtle.Aswellasturtle,youcanalsouse
“arrow”,“circle”,“square”,“triangle”or
“classic”.
speed(5)
tellstheturtlehowfasttodraw.You
canuseanumberbetween1and11.11isthe
fastest,1istheslowest.
forward(100)
and backward(100) tellsthe
turtletomoveforwardorbackward100pixels.
left(45)
and right(90) turntheturtleleft
orrightbyanumberofdegrees.Hereare
someexamples:
done()
tellsPythonthatwe’vefinished
programmingtheturtle.
3. What’syourfavouritecolour?Tomakeyourdrawingsmore
interesting,youcanalsochangethecolourandthesizeofthe
pendrawingtheline.Here’sasimpleexampletotry:
fromturtleimport*
shape("turtle")
3
TheseprojectsareforuseoutsidetheUKonly.Moreinformationisavailableonourwebsiteatwww.codeclubworld.org.
ThiscourseworkisdevelopedintheopenonGitHub,atwww.github.com/CodeClub.Comeandjoinus!
speed(8)
color("Purple")
pensize(7)
right(90)
forward(100)
left(90)
forward(50)
color("Orange")
pensize(3)
penup()
forward(50)
pendown()
forward(50)
done()
4. Thecodeabovecontainsacoupleofnewcommands:
color("Purple")
turnstheturtleandtheline
purple.NoticetheAmericanspellingofthe
wordcolour,whichdoesn’thavea‘u’init.You
canalsospecifycoloursinhex,likeyoudidin
CSS.Insteadofusing pencolor("Red") you
coulduse pencolor("#FF0000") .
penup()
liftsthepenfromthescreen,and
pendown()
lowersitagain.Thismeansthatyou
canmovetheturtlewithoutleavingatrail!
4
TheseprojectsareforuseoutsidetheUKonly.Moreinformationisavailableonourwebsiteatwww.codeclubworld.org.
ThiscourseworkisdevelopedintheopenonGitHub,atwww.github.com/CodeClub.Comeandjoinus!
SaveYourProject
Challenge:Drawingshapes
Canyouusetheturtleinstructionsabovetodraw:
Asquare?
Atriangle?
Canyoudrawahouse?Whatelsecanyoudraw?
SaveYourProject
Step2:Repeatingyourself
Whendrawingasquareandatriangle,yourprogramrepeatedthesame
commandsoverandoveragain.Let’sgetPythontorepeatthemforus!
ActivityChecklist
1. Openupanewfile,andrunthefollowingprogram:
fromturtleimport*
speed(11)
shape("turtle")
forcountinrange(4):
forward(100)
right(90)
done()
Thisprogramusesa for loop.Youcanusea for loopin
5
TheseprojectsareforuseoutsidetheUKonly.Moreinformationisavailableonourwebsiteatwww.codeclubworld.org.
ThiscourseworkisdevelopedintheopenonGitHub,atwww.github.com/CodeClub.Comeandjoinus!
Pythonwheneveryouwanttorepeatsomecodeasetnumber
oftimes.
Intheprogramabove,thecommands forward(100) and
right(90)
arerepeated4times,drawingasquare.Turning90
degreesforeachcornermeansweturn360degreesintotal.
2. Justlikewithan if statement,youshouldusetheTabkeyto
indentthecodethatyouwanttorepeat.Trychangingthe
code,sothattheline forward(100) isindentedbuttheline
right(50)
isn’t,likethis:
fromturtleimport*
speed(11)
shape("turtle")
forcountinrange(4):
forward(100)
right(90)
done()
Whathappenswhenyourunthisprogram?Didyougeta
straightline?Inthisprogram,Pythonwillrepeat forward(100)
fourtimes,andthenturn right(90) .
3. Nowthatyouknowhowtorepeatcommands,youcancreate
complicatedshapesandpatternsreallyeasily.Runthis
program:
fromturtleimport*
speed(11)
shape("turtle")
forcountinrange(8):
6
TheseprojectsareforuseoutsidetheUKonly.Moreinformationisavailableonourwebsiteatwww.codeclubworld.org.
ThiscourseworkisdevelopedintheopenonGitHub,atwww.github.com/CodeClub.Comeandjoinus!
forward(100)
right(45)
done()
Thisprogramworksinthesamewayasthesquaredrawing
program,exceptthatitrepeats8times,andonlyturns45
degreesforeachcorner.Thismeansthatthecodedrawsan
8-sidedshape(anoctagon),asthecornersforeachofthe8
sidesaddupto360degrees(360dividedby8is45).
4. Here’sanotherexampleofwhatcanbecreatedusinga for
loop.Whatdoesthisprogramdraw?
fromturtleimport*
speed(11)
shape("turtle")
forcountinrange(30):
forward(5)
penup()
forward(5)
pendown()
7
TheseprojectsareforuseoutsidetheUKonly.Moreinformationisavailableonourwebsiteatwww.codeclubworld.org.
ThiscourseworkisdevelopedintheopenonGitHub,atwww.github.com/CodeClub.Comeandjoinus!
done()
SaveYourProject
Challenge:Loopyshapes
Canyouusea for looptodraw:
Apentagon?(fivesides)
Ahexagon?(sixsides)
Rememberthattheanglesofallthecornersalways
addupto360degrees!
Canyoudrawacircle?Youcanmoveforward1pixeland
turn1degreeeachtime.Howmanytimeswouldyouneed
torepeatthesecommands?
SaveYourProject
8
TheseprojectsareforuseoutsidetheUKonly.Moreinformationisavailableonourwebsiteatwww.codeclubworld.org.
ThiscourseworkisdevelopedintheopenonGitHub,atwww.github.com/CodeClub.Comeandjoinus!
Challenge:Drawingpatterns
Canyouusewhatyou’velearnttodrawawesomepatterns?
Here’sanexample:
fromturtleimport*
speed(11)
shape("turtle")
pensize(6)
color("Red")
forcountinrange(36):
forward(100)
right(100)
done()
SaveYourProject
9
TheseprojectsareforuseoutsidetheUKonly.Moreinformationisavailableonourwebsiteatwww.codeclubworld.org.
ThiscourseworkisdevelopedintheopenonGitHub,atwww.github.com/CodeClub.Comeandjoinus!
Challenge:Variablesandloops
Whendrawingdifferentshapes,youhadtocalculatehowmany
degreestoturnforeachcorneryourself.
Canyouuseacalculation,sothatthecomputerworksthisout
foryou?Toworkoutthenumberofdegreestoturn,youcan
divide360bythenumberofsidesintheshape:
sides=4
angle=360/sides
/
isthePythonsymbolfordivide.Noticethattheansweris
storedinavariablecalled angle ,whichyoucanthenuseto
drawyourshape:
left(angle)
Youcanthenchangethenumberstoredinthe sides variable
andtestthatitworksforanyshape!
SaveYourProject
10
TheseprojectsareforuseoutsidetheUKonly.Moreinformationisavailableonourwebsiteatwww.codeclubworld.org.
ThiscourseworkisdevelopedintheopenonGitHub,atwww.github.com/CodeClub.Comeandjoinus!