Python Programming for Raspberry Pi, Sams Teach Yourself in 24 Hours

(singke) #1
Notice that no input verification code for entered member names is present. That will
be added in the next section of this hour.


  1. Test your new script in IDLE by pressing the F5 key and entering answers to the
    questions. Your results should look similar to the results in Figure 7.5.


FIGURE 7.5 The script0702.py script output.


  1. If you want to save this script, press Ctrl+S to open the Save As window, double-
    click the py3prog folder icon, type script0702.py in the File Name bar, and
    then click the Save button.

  2. Exit the IDLE environment by pressing Ctrl+Q.
    No input verification is done on the while loop that you entered in this section. In the
    next part of this hour, you will see how to clean up script0702.py by using a
    nested loop.


Creating Nested Loops


A nested loop is a loop statement that is inside a loop statement. For example, a for loop used
within the code block of a while loop would be a nested loop. Listing 7.21 shows a script that uses
nested loops. It has a for loop that contains three while loops in the for loop’s code block.
script0703.py is a slight improvement over the script you wrote in the last Try it Yourself
section of this hour.


LISTING 7.21 A Nested Loop in script0703.py


Click here to view code image


1: pi@raspberrypi ~ $ cat py3prog/script0703.py
2: # script0703.py - Demonstration of a nested loop.
3: # Author: Blum and Bresnahan
4: # Date: May 01
5: ###########################################################
6: #
Free download pdf