Download presentation
Presentation is loading. Please wait.
1
Text Copyright (c) 2017 by Dr. E. Horvath
Loops: Infinite Loops In order to execute statements over and over again, use a while loop or a for loop. The simplest kind of loop is the while loop, and an example is as follows while True: print('Welcome to Python with Raspberry PI') This print statement executes until you press ctrl-c and you'll see feedback indicating that there is a KeyboardInterrupt. Although we will not discuss the try-except construct until later in the course, we will introduce it on the following page because it offers a tidy way of exiting from the loop. Text Copyright (c) 2017 by Dr. E. Horvath
2
Loops: Infinite Loops Escape Handling
try: while True: print('Welcome to Python with Raspberry PI') except KeyboardInterrupt: print('Escaping loop') Inside the except block is where you incorporate code to handle a keyboard interrupt caused by pressing ctrl-c. There is nothing to clean up here which is why there is just a print statement. If you want to catch the exception, but you don't want any statement executed, add the pass statement instead. Text Copyright (c) 2017 by Dr. E. Horvath
3
Loops: Allowing the Program to Sleep
If you wish to poll for sensor readings,e.g., temperature, pressure, and humidity, you might use a while loop of the kind shown on the previous two slides. However, continually polling the sensors and processing the data will tax the CPU. A better way is to allow the program to rest using the sleep command. from time import sleep from sense_hat import SenseHat sense = SenseHat() while True: temp = sense.get_temperature() sleep(1) #The argument is in units of seconds Text Copyright (c) 2017 by Dr. E. Horvath
4
Loops: Sleep and Interrupt Handling
A while loop example which incorporates the features discussed on the previous slides is given below. from time import sleep from sense_hat import SenseHat sense = SenseHat() try: while True: temp = sense.get_temperature() sleep(1) except KeyboardInterrupt: print('Escaping loop') Text Copyright (c) 2017 by Dr. E. Horvath
5
Loops: Finite while loops
While loops can also be used to execute for a finite number of times or until some condition no longer exists. Let's consider the first kind. i = 0 while i < 10: print(str(i)) i += 1 If the programmer forgets to increment the dummy index, i, then this will be an infinite loop, unintentionally so. Also, the Python interpreter will throw an error if the programmer neglects to initialize the dummy index. Text Copyright (c) 2017 by Dr. E. Horvath
6
Loops: Finite while loops
Let's suppose the while loop ends only when some condition no longer exists. In the following, the loop executes only as long as the humidity is below 75%. from sense_hat import SenseHat from time import sleep sense = SenseHat() humidity = 0 while humidity < 75: humidity = sense.get_humidity() print('Humidity ',humidity) sleep(2) Text Copyright (c) 2017 by Dr. E. Horvath
7
Loops: Finite while loops
Suppose the humidity never rises above 75% then the program will not escape from the loop until ctrl-c is pressed. The programmer could add an additional condition that will cause the program to exit from the loop after a prescribed number of steps. from sense_hat import SenseHat from time import sleep sense = SenseHat() Humidity = 0 i = 0 while humidity < 75 and i < 1000: humidity = sense.get_humidity() print('Humidity ',humidity) sleep(2) i += 1 Text Copyright (c) 2017 by Dr. E. Horvath
8
Text Copyright (c) 2017 by Dr. E. Horvath
Loops: Breaking out The user is prompted for an elementary particle. If he answers electron, proton, or neutron then the break statement is executed. while True: particle = input('Guess an elementary particle') if particle == 'electron' or particle == 'proton' or \ particle == 'neutron': print('Congratulations! You're right. A(n)', particle, \ 'is an elementary particle') break else: print('Sorry, that's wrong. \ Or maybe that is an elementary particle. Try again.') Text Copyright (c) 2017 by Dr. E. Horvath
9
Text Copyright (c) 2017 by Dr. E. Horvath
Loops: Nested Loops Both dummy indices must be initialized before the while loops are entered. Notice where the indices are initialized and notice where each dummy index is incremented. The dummy index, j, is incremented inside the while j loop and the dummy index, i, is incremented inside the while i loop, but after the while j loop. i = 0 while i < 5: j = 0 while j < 8: print('i= '+ i + ' j= ' + j) j += 2 i += 1 Text Copyright (c) 2017 by Dr. E. Horvath
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.