Download presentation
Presentation is loading. Please wait.
Published byElyssa Capshaw Modified over 9 years ago
1
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: www.cs.uml.edu/~kim/100.html How to create a turtle How to move it and change properties
2
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
3
Simpler way of handling repetition ? => Functions Abstraction Black Box Container for a sequence of actions Use the function by name
4
Figure 1.8
5
Defining Functions Name Parameters Body
6
Listing 1.1 def functionName(param1,param2,...) : statement1 statement2...
7
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
8
Figure 1.10
9
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
10
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)”
11
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
12
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) …
13
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) …
14
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 ?
15
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
16
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.