1 / 10

Simple Python Loops

Simple Python Loops. Sec 9-7 Web Design. Objectives. The student will: Know how to program simple “for” loop in Python Understand the structure of a “while” loop in Python Know how loop for a certain period of time Know how to loop forever. For loop.

uma-garza
Télécharger la présentation

Simple Python Loops

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. Simple Python Loops Sec 9-7 Web Design

  2. Objectives The student will: • Know how to program simple “for” loop in Python • Understand the structure of a “while” loop in Python • Know how loop for a certain period of time • Know how to loop forever

  3. For loop • The simplest form of repetition is a for Loop. • Remember from Alice LOOP repeated a fixed number of times. In Python that is called a for loop • The basic syntax of a for-loop in Python is: for <variable> in <sequence>: <do something> <do something> ...

  4. For Loop • The loop specification begins with the keyword for which is followed by a <variable> and then the keyword in and a <sequence> followed by a colon(:). • This line sets up the number of times the repetition will be repeated. • What follows is a set of statements, indented (again, indentation is important), that are called a block that forms the body of the loop (stuff that is repeated).

  5. For Loop • When a for loop is executed, the <variable> (which is called a loop index variable) isassigned successive values in the <sequence> and for each of those values, the statements in the body of the loop are executed. • A <sequence> in Python is a list of values • To execute a loop a certain number of times you can use the range() function. • To see what this function does you can start IDLE and type: >>> range(10) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] • The range function simply returns a list of numbers from 0 to one less than the number passed to the function

  6. For Loop Example • If I have a dance function • I want to repeat it 10 times for i in range(10): dance() • iis a variable. Each time the loop is executed i has a different value (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)

  7. Looping Forever • In writing robot programs there will be times when you just want the robot to keep doing its behaviors forever. While technically by forever we do mean eternity in reality what is likely to happen is either it runs out of batteries, you decide to stop it (by hitting CTRL-C), or the period ends and you turn it off. • The Python command to specify this uses a different loop statement, called a while-loop that can be written as: while True: <do something> <do something> ... • We will cover more of while-loops later.

  8. Repetition Using Time • In addition to repeating by counting or looping forever, you can also specify repetition using time. while timeRemaining(<how_many_seconds>): <do something> <do something> ... • If you wanted the computer to say “Doh!” for 5 seconds, you can write: while timeRemaining(5): speak("Doh!", 0)

  9. Summary • There are multiple ways to loop: • Repetitions: for <variable> in <sequence>: • Loop forever: while True: • Loop for a certain time: while timeRemaining(secs):

  10. Rest of Today • Take your octagon program. • Use a simple loop to create the octagon rather than having the commands repeat over and over. • Show me the program when complete.

More Related