
Python Looping Patterns and Event Handling
Explore Python coding patterns such as the Wait-Until-Event and Definite Loop patterns, illustrated with examples and images. Learn how to control loops based on specific events and conditions.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
The Wait-Until-Event pattern Has it happened Has it happened Has it happened Has it happened Yes, it happened!
The Definite Loop pattern for k in range(...): ... ... window = zg.GraphWin('Animation', 650, 430) x = 20 y = 50 radius = 5 for k in range(150): circle = zg.Circle(zg.Point(x, y), radius) circle.setFill('purple') circle.draw(window) Determines how many times the loop iterates The body of the loop x = x + 2 y = y + 1 radius = radius + k / 100 time.sleep(0.01)
The Definite Loop pattern for k in range(...): ... ... window = zg.GraphWin('Animation', 650, 430) x = 20 y = 50 radius = 5 for k in range(150): circle = zg.Circle(zg.Point(x, y), radius) circle.setFill('purple') circle.draw(window) x = x + 2 y = y + 1 radius = radius + k / 100 time.sleep(0.01)
The Definite Loop pattern The Wait-Until-Event pattern Repeatedly: ... Has the event occurred? If so, break out of the loop. ... Run n times: ... ...
The Wait-Until-Event pattern Repeatedly: ... Has the event of interest occurred? If so, break out of the loop. ... Robot: Start moving. Repeatedly: Robot: Have you bumped into anything? If so, break out of the loop. Repeatedly: x = something input from the user if x is the sentinel value: break out of the loop Processx. Robot: Stop. 463 814 22 -1
The Wait-Until-Event pattern Expressed in Python using while True and break while True: ... if the event of interest occurred: break ... robot.go(..., 0) while True: sensor = 'BUMPS_AND_WHEEL_DROPS' bump = robot.getSensor(sensor) if bump[3] == 1 or bump[4] == 1: break while True: msg = 'Enter int, or -1 to stop' x = int(input(msg)) if x == -1: break Processx. robot.stop()
The Definite Loop pattern for k in range(...): ... ... Do the following 150 times: Draw a circle. Move and grow the circle. while True: ... if the event of interest occurred: break ... The Wait-Until-Event pattern Repeatedly draw, move and grow a circle, stopping when the circle grows beyond the border of the window. Do blah until the user asks you to stop. Robot, go until you hit a wall. Robot, go until your sensors say that you have gone 10 centimeters.
The Wait-Until-Event pattern while True: ... if the event of interest occurred: break ... Repeatedly draw, move and grow a circle, stopping when the circle grows beyond the border of the window. Do blah until the user asks you to stop. Robot, go until you hit a wall. Robot, go until your sensors say that you have gone 10 centimeters.