MoreExerciseswithLoopsCS8:IntroductiontoComputerScience,Spring2019
Lecture#8
ZiadMatni,Ph.D.Dept.ofComputerScience,UCSB
Administrative• Hw04–duenextweek• Lab02–dueonSundaybymidnight(11:59pm)onGradescope!
• MidtermExam#1isNEXTWEEKonWed.,May2nd– I’llputupsampleproblemsafterWed.thisweek
4/25/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/25/19 Matni,CS8,Wi19 3
Midterm#1ExamWHAT’SONIT?!
• Everything– ReviewALLlectures– ReviewALLreadings– ReviewALLlabs– ReviewALLhomework
4/25/19 Matni,CS8,Wi19 4
Midterm#1ExamSAMPLEQUESTIONS?!?!?!?!?!?!
• Yes!SeeStudyGuideontheclasswebsite!
4/25/19 Matni,CS8,Wi19 5
LectureOutline• Moreexercisewithloopsusingforandwhile
4/25/19 Matni,CS8,Sp19 6
Exercise1• Whatwillthiscodedo?forpinrange(6,25,6):ifp%12==0: print("X",end="")else: print("O",end="")
4/25/19 Matni,CS8,Sp19 7
Exercise2• Whatwillthiscodedo?sum=0count=0while(count<10):ifcountin(3,8): print(count/2)count+=1sum+=count
print(count,sum,sep=",")
4/25/19 Matni,CS8,Sp19 8
ClassExerciseGettogetherwith2or3otherpeoplearoundyouandanswerthisquestion.
Youcanuseyournotesfromlasttime.Youcanuseyourcomputers:
Aprimenumberisapositive,non-zerointegerthatcannotbedividedbyanyotherpositive,non-zerointeger,exceptONE(1)andITSELF.
Forexample,5isaprimenumber,but6isnot(it’sdivisibleby2andby3).Thefirstfiveprimenumbersare:2,3,5,7,and11
WriteaPythonfunction,prime(),thattakesanargumentnandreturnseitherTrue(ifnisaprimenumber)orFalse(ifnisNOTaprimenumber).
4/25/19 Matni,CS8,Wi19 9
ClassExercisedefprime(n):
p=Trueforiinrange(2,n): ifn%i==0: p=Falsereturnp
4/25/19 Matni,CS8,Wi19 10
ClassExercisedefprime(n):
p=Trueforiinrange(2,n): ifn%i==0: p=Falsereturnp
#Let’stestournewfunctionout!foriinrange(2,3000):
ifprime(i): print(i)
4/25/19 Matni,CS8,Wi19 11
BONUS:print(i,",",end="")
Exercise3TakeoutapieceofpaperandwritePythoncodethatwillgothroughallthenumbersfrom1to10(inclusiveof1and10)andprintsouttheproductofallthesenumbers.YouMUSTuseafor-looptosolvethisproblem.
4/25/19 Matni,CS8,Sp19 12
Exercise3-Solutionp=1forninrange(1,11): p=p*n #orp*=n
print(p)
4/25/19 Matni,CS8,Sp19 13
Exercise4–NestedLoops
4/25/19 Matni,CS8,Sp19 14
• Whatwillthiscodedo?forpinrange(2):forqinrange(3): print("z",end="")
IntroducingTurtleGraphics!• AnicewaytogetintroducedtosimplegraphicsusingPython
• Youhavetofirstimportturtle
• YoucanthenuseitasperthedemoI’mabouttogive…
4/25/19 Matni,CS8,Sp19 15
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/25/19 Matni,CS8,Sp19 16
WhatWillTheseDo?importturtleboris=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)4/25/19 Matni,CS8,Sp19 17
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)
YOURTO-DOsq FinishreadingChapter5q FinishHW4(dueTUESDAY)q FinishLab2(turnitinbySunday)
q Whistlewhileyouwork
4/25/19 Matni,CS8,Sp19 18
4/25/19 Matni,CS8,Sp19 19