More Exercises with Loops Turtle Graphics · More Exercises with Loops Turtle Graphics CS 8:...
Transcript of More Exercises with Loops Turtle Graphics · More Exercises with Loops Turtle Graphics CS 8:...
-
MoreExerciseswithLoopsTurtleGraphics
CS8:IntroductiontoComputerScience,Spring2019Lecture#8
ZiadMatni,Ph.D.
Dept.ofComputerScience,UCSB
-
Administrative• Nohomeworkthisweek!• Lab03–dueonSundaybymidnight(11:59pm)onGradescope!
• MidtermExam#1isonWednesday!
4/30/19 Matni,CS8,Sp19 2
-
Midterm#1Exam• May2nd,2:00–3:15PMinTHISclassroom(unlessyouareaDSPstudent)• Come10MINUTESEARLYastheremightbepre-assignedseating• CLOSEDBOOK!Butyoucanbring1pageofnotes
– Single-sideonly,8.5”x11”– Hand-writtenorcomputerprintedisOK!– Mustturnitinwiththeexamwhendone– Nocalculators/cellphones/anytypeofcomputer
• BringyourUCSBIDwithyou.NOEXCEPTIONS.
4/30/19 Matni,CS8,Sp19 3
-
Midterm#1ExamWHAT’SONIT?!
• Everything– ReviewALLlectures– ReviewALLreadings– ReviewALLlabs– ReviewALLhomework
4/30/19 Matni,CS8,Sp19 4
-
Midterm#1ExamSAMPLEQUESTIONS?!?!?!?!?!?!
• Yes!SeeStudyGuideontheclasswebsite!
4/30/19 Matni,CS8,Sp19 5
-
LectureOutline• Moreexercisewithloopsusingforandwhile
4/30/19 Matni,CS8,Sp19 6
-
Re:MutabilityofVariables• Rememberthat“immutable”variablesarenot“unchangeable”
– Eg.int,str,float,etc…
• TheyCANbechanged,iftheyareRE-ASSIGNED– INSIDEafunction
• Whateverchangeswemakeinsideafunctionwillremaininsidethefunction– ButwecanalwaysRETURNthesevariablessothatthechangesare
reflectedoutsidethefunction
4/30/19 Matni,CS8,Sp19 7
-
Re:MutabilityofVariablesdefswap(a,b):temp=aa=bb=temp
x=3y=33swap(x,y)#what’sx?y?
4/30/19 Matni,CS8,Sp19 8
defswap(a,b):temp=aa=bb=tempreturna,b
x=3y=33x,y=swap(x,y)#what’sx?y?
-
IntroducingTurtleGraphics!• AnicewaytogetintroducedtosimplegraphicsusingPython
• Youhavetofirstimportturtle
• YoucanthenuseitasperthedemoI’mabouttogive…
4/30/19 Matni,CS8,Sp19 9
-
BasicTurtleCommandsimportturtletimmy=turtle.Turtle()#Settheturtleobject,callittimmy!timmy.forward(100) #Drawforwards100pixelstimmy.right(90) #Turntheturtle90degreestotherighttimmy.backwards(50) #Drawbackwards50pixelstimmy.left(45) #Turntheturtle45degreestothelefttimmy.color("blue")#Maketimmybluetimmy.pensize(3) #Setthewidthofthepentimmy.penup() #Putpenup(canmoveitw/odrawing)timmy.pendown() #Putpendown(candrawagain)4/30/19 Matni,CS8,Sp19 10
-
WhatWillTheseDo?importturtle
boris=turtle.Turtle()
boris.color("blue")
boris.forward(100)
boris.right(90)
boris.forward(100)
boris.right(90)
boris.forward(100)
boris.right(90)
boris.forward(100)
boris.right(90)Matni,CS8,Sp19 11
importturtlenatascha=turtle.Turtle()natascha.color("red")natascha.forward(100)natascha.left(60)natascha.forward(100)natascha.left(60)natascha.forward(100)natascha.left(60)natascha.forward(100)natascha.left(60)natascha.forward(100)natascha.left(60)natascha.forward(100)natascha.left(60)
-
SimplerDrawingByRepetition• DrawingasquareusingTurtleandloops!
defdrawSquare2(myTurtle,sideLength):foriinrange(4):myTurtle.forward(sideLength)myTurtle.right(90)
4/30/19 Matni,CS8,Sp19 12Let’s try these out!
-
MoreDrawingAbstraction• DrawingatriangleusingTurtleandloops!
defdrawTriangle(myTurtle,sideLength):foriinrange(3): #draw3sides,not4myTurtle.forward(sideLength)myTurtle.right(120) #120°×3
4/30/19 Matni,CS8,Sp19 13Let’s try these out!
-
MoreDrawingAbstraction• DrawinganyregularpolygonusingTurtleandloops!
defdrawPolygon(myTurtle,sideLength,numSides):turnAngle=360/numSidesforiinrange(numSides):myTurtle.forward(sideLength)myTurtle.right(turnAngle)
4/30/19 Matni,CS8,Sp19 14Let’s try these out!
-
SimplerDrawingByRepetition• DrawingaspiralusingTurtleandloops!
defdrawSpiral(myTurtle,maxSide):forsideLengthinrange(1,maxSide+1,5):myTurtle.forward(sideLength)myTurtle.right(90)
4/30/19 Matni,CS8,Sp19 15Let’s try these out!
-
ExampleforLoopusingaString• Whatdoyouthinkthiscodedoes?s="Takemehome,countryroads"forcins:ifcin('a','e','i','o','u'): print("Vowelfound:",c)
4/30/19 Matni,CS8,Sp19 16
-
Exampleforloopusingstring• Whatdoyouthinkthiscodedoes?s="Takemehome,countryroads"t=0forcins:ifcin('a','e','i','o','u'): t+=1
print("Therewere",t,"vowelsfound")
4/30/19 Matni,CS8,Sp19 17
#Accumulatedsum
#Set-upforanaccumulatedsum
-
Exampleforloopusingstring• Whatdoyouthinkthiscodedoes?s="TAKEMEHOME,COUNTRYROADS"t=0forcins:ifcin('a','e','i','o','u'): t+=1
print("Therewere",t,"vowelsfound")
4/30/19 Matni,CS8,Sp19 18
#Accumulatedsum
#Set-upforanaccumulatedsum
-
NestedLoops
4/30/19 Matni,CS8,Sp19 19
• Whatwillthiscodedo?forpinrange(2):forqinrange(3): print("z",end="")
-
NestedLoops• Whatwouldthisdo?listX=[[1,2,3], [4,5,6,7,8,9], ["a","b","c"]]
foriinlistX:forjini: print(j,end="")
4/30/19 Matni,CS8,Sp19 20
-
YOURTO-DOsq Studyforthemidterm!q NoHomeworkthisweek!q FinishLab3(turnitinbySunday)
q Ensure(smiles–frowns)>0
4/30/19 Matni,CS8,Sp19 21
-
4/30/19 Matni,CS8,Sp19 22