By Now, 1. Your Student ID should open the classroom door 2. You should have C:\100 folder Desktop shortcuts to Command Prompt and Notepad 3. You should know Class webpage: How to create a turtle How to move it and change properties
Draw a Square >>> import turtle >>> myTurtle = turtle.Turtle() >>> myTurtle.forward(100) >>> myTurtle.right(90) # side 1 >>> myTurtle.forward(100) >>> myTurtle.right(90) # side 2 >>> myTurtle.forward(100) >>> myTurtle.right(90) # side 3 >>> myTurtle.forward(100) >>> myTurtle.right(90) # side 4
Simpler way of handling repetition ? => Functions Abstraction Black Box Container for a sequence of actions Use the function by name
Figure 1.8
Defining Functions Name Parameters Body
Listing 1.1 def functionName(param1,param2,...) : statement1 statement2...
Listing 1.2 def drawSquare(myTurtle): myTurtle.forward(100) myTurtle.right(90) # side 1 myTurtle.forward(100) myTurtle.right(90) # side 2 myTurtle.forward(100) myTurtle.right(90) # side 3 myTurtle.forward(100) myTurtle.right(90) # side 4
Figure 1.10
How to Use a Function 1 1. Open a Notepad, and enter function statements 2. Save the file with.py extension Such a file is called a module, script, function Save it as drawS.py in C:\100
How to Use a Function 2 3. Open a Command Window “cd../../100” to bring the system to C:\100 folder “python” “>>> import turtle” “>>> myT = turtle.Turtle()” “>>> import drawS” “>>> drawS.drawSquare(myT, 100)”
Questions File name is drawS.py But we use ‘drawS.drawSquare() Parameter name is myTurtle But we use drawS.drawSquare(myT) def drawSquare(myTurtle): myTurtle.forward() myTurtle.right(90) … drawS.py
Arguments are placeholders Think of an argument as a placeholder It takes the place of the actual input object Only when a function is executed, the input object takes actual values replacing the argument def drawSquare(myTurtle): myTurtle.forward(100) myTurtle.right(90) …
What if we want a function drawS() to draw a square of different sizes ? From To def drawSquare(myTurtle, sideLength): myTurtle.forward(sideLength) myTurtle.right(90) … def drawSquare(myTurtle): myTurtle.forward(100) myTurtle.right(90) …
An imaginary wedding computer def marry(husband, wife): sayVows(husband) sayVows(wife) pronounce(husband, wife) kiss(husband, wife) def sayVows(speaker): print “I, “ + speaker + “ blah blah” def pronounce(man, woman): print “I now pronounce you…” def kiss(p1, p2): if p1 == p2: print “narcissism!” if p1 <> p2: print p1 + “ kisses “ + p2 So, how do we marry Ben and Jo ?
An imaginary wedding computer def marry(husband, wife): sayVows(husband) sayVows(wife) pronounce(husband, wife) kiss(husband, wife) def sayVows(speaker): print “I, “ + speaker + “ blah blah” def pronounce(man, woman): print “I now pronounce you…” def kiss(p1, p2): if p1 == p2: print “narcissism!” if p1 <> p2: print p1 + “ kisses “ + p2
An imaginary wedding computer def marry(husband, wife): sayVows(husband) sayVows(wife) pronounce(husband, wife) kiss(husband, wife) def sayVows(speaker): print “I, “ + speaker + “ blah blah” def pronounce(man, woman): print “I now pronounce you…” def kiss(p1, p2): if p1 == p2: print “narcissism!” if p1 <> p2: print p1 + “ kisses “ + p2