SlideServe
Menu
Préférences
© SlideServe · v2026

Iteration

mina avatar
17 diapositives · 349 Views · Oct 21, 2014

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 )

Iteration - slide 1
01 / 17Iteration
1 / 17

Télécharger la présentation

Iteration
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  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