1 / 16

Draw a Square

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)

robin-sweet
Download Presentation

Draw a Square

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. Simpler way of handling repetition ? • => Functions • Abstraction • Black Box • Container for a sequence of actions • Use the function by name

  3. Figure 1.8

  4. Defining Functions • Name • Parameters • Body

  5. Listing 1.1 deffunctionName(param1,param2,...): statement1 statement2 ...

  6. Listing 1.2 defdrawSquare(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

  7. Figure 1.10

  8. How to Use a Function 1 • Create a new folder C:\100 • Save the function into a file with .pyextension • Such a file is called a module, script, function • Open a notepad (Start>Accessories>Notepad) • Right click on notepad -> Select Send to Desktop • Type in Python statements • Save it as drawS.py in C:\100

  9. How to Use a Function 2 • Open a Command Window (Start>Accessories) • Right click on Command Prompt to Desktop • In the Command Window • “cd ../../100” to bring the system to C:\100 folder • “python” • “>>> import turtle” • “>>> myT = turtle.Turtle()” • “>>> import drawS” • “>>> drawS.drawSquare(myT, 100)”

  10. Questions • File name is drawS.py • But we use ‘drawS.drawSquare() • Parameter name is myTurtle • But we use drawS.drawSquare(myT) drawS.py def drawSquare(myTurtle): myTurtle.forward() myTurtle.right(90) …

  11. 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) …

  12. What if we want a function drawS() to draw a square of different sizes ? • From • To def drawSquare(myTurtle): myTurtle.forward(100) myTurtle.right(90) … def drawSquare(myTurtle, sideLength): myTurtle.forward(sideLength) myTurtle.right(90) …

  13. 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 ?

  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

  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. Figure 1.11

More Related