1 / 17

Iteration

Iteration. When statements are repeated, any simpler way ? A for statement with a range Tell Python that I have an iteration Tell Python how many times. def drawSquare ( myTurtle,sideLength ): myTurtle.forward ( sideLength ) myTurtle.right (90) # side 1 myTurtle.forward ( sideLength )

mina
Télécharger la présentation

Iteration

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. Iteration • When statements are repeated, any simpler way ? • A for statement with a range • Tell Python that I have an iteration • Tell Python how many times defdrawSquare(myTurtle,sideLength): myTurtle.forward(sideLength) myTurtle.right(90) # side 1 myTurtle.forward(sideLength) myTurtle.right(90) # side 2 myTurtle.forward(sideLength) myTurtle.right(90) # side 3 myTurtle.forward(sideLength) myTurtle.right(90) # side 4

  2. Listing 1.3 defdrawS(myT,sideLength): for i in range(0,4): myT.forward(sideLength) myTurtle.right(90) defdrawSquare(myTurtle,sideLength): myTurtle.forward(sideLength) myTurtle.right(90) # side 1 myTurtle.forward(sideLength) myTurtle.right(90) # side 2 myTurtle.forward(sideLength) myTurtle.right(90) # side 3 myTurtle.forward(sideLength) myTurtle.right(90) # side 4

  3. FOR loop • “for i in range(0,4):” • “i” – a dummy variable keeping counts • range(a, b): • a, a+1, a+2, ….. , b-2, b-1 • Do not forget “:” at the end • Statements to be repeated have to be indented

  4. Figure 1.12

  5. Another example • Five numbers to get an average: 20, 30, 40, 5, 55 • A general way of getting an average of any five numbers ? • A collection of five numbers – how to tell what it is ? >>> avg2 = (20+30+40+5+55)/5

  6. Example • Compute the class average Initialize total to zero Initialize counter to zero Input the first grade while the user has not as yet entered the sentinel add this grade into the running total add one to the grade counter input the next grade (possibly the sentinel) if the counter is not equal to zero set the average to the total divided by the counter print the average else print 'no grades were entered'

  7. Example • Compute the class average total = 0 counter = 0 while grades[counter] >=0: total = total + grades[counter] counter = counter+1 if counter > 0: average = total/counter print average else: print 'no grades were entered'

  8. Average • List – an ordered set • Index starting with 0 identifies each item in a list • A general way of getting an average of any five numbers ? • But, would be nice to ‘automate’ the summing process • => Further abstraction >>> myList = [20,30,40,5,55] >>> avg1 = (myList[0]+myList[1]+myList[2]+myList[3]+myList[4])/5

  9. Getting the sum sum = myList[0]+myList[1]+myList[2]+myList[3]+myList[4] • How to automate the sum ? sum = 0 sum = sum + myList[0] sum = sum + myList[1] sum = sum + myList[2] sum = sum + myList[3] sum = sum + myList[4]

  10. Getting the sum sum = 0 sum = sum + myList[0] sum = sum + myList[1] sum = sum + myList[2] sum = sum + myList[3] sum = sum + myList[4] • Haven’t really automate the process, but we have sum = 0 sum = sum + myList[ ] sum = sum + myList[ ] sum = sum + myList[ ] sum = sum + myList[ ] sum = sum + myList[ ]

  11. “for” loop sum = 0 sum = sum + myList[0] sum = sum + myList[1] sum = sum + myList[2] sum = sum + myList[3] sum = sum + myList[4] • “for” steps through every element sum = 0 forin range(0, 5): sum = sum + myList[ ] • How about ? • Use any variable name

  12. “for” loop for p in range(min, max): block p=min true p = max false Do block p = p +1 Out of “for”

  13. Program an average • Remember getting an average ? • Can you write a command (in ???? below) to compute the average using ‘psum’ >>> myList = [20,30,40,5,55] >>> avg1 = (myList[0]+myList[1]+myList[2]+myList[3]+myList[4])/5 >>> myList = [-20, 50, -4, 20, 7] >>> n=5 >>> psum = 0 >>> for i in range(0,n): … psum = psum + myList[i] … >>> ????

  14. sum Function • Hate typing an array of values, “for i in range(,):” each time you compute a sum • Any easier way ? -- YES !! • Create a GENERIC function to compute a sum regardless of arrays of values • => Further ABSTRACTION

  15. sum Function def sum(thisList): n = len(thisList) psum = 0 for j in range(0,n): psum = psum+thisList[j] return psum

  16. Listing 1.4 def drawSpiral(myTurtle,maxSide): for sideLength in range(1,maxSide+1,5): myTurtle.forward(sideLength) myTurtle.right(90)

  17. Figure 1.13

More Related