Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
the while and else structure

All the languages we teach have if and else conditionals, and while loops. They're a necessary part of any language, accepted as something of a standard, and we teach them on all the fundamental courses.

Uniquely, Python allows you to add an else clause onto the end of a while loop. Why? So that you can define a block of code that's to be run upon an exit from the loop caused by the loop's condition becoming false, but that's to be skipped over if the loop exits through a break statement or because of an exception.

Here's an example. This program is going to loop, reading in people who are coming to your teenager's party. If you complete the data entry by pressing the "enter" key without entering another name, the <b>else</b> clause on the while is run and your party list is displayed. If you enter "parents" then the party plans are abandoned - you break out of the data entry loop and don't run the else block.

running = 1
friends = []

while running:
    said = raw_input("Who's coming to the party? ")
    if said == "parents":
        print "Party plans abandoned"
        break
    if said == "":
        print "Data entry complete"
        running = 0
    else:
        friends.append(said)
else:
    print "Having a party for ",
    print friends
print "program exiting"

Let's see what that does when we run it

earth-wind-and-fire:~/feb05 grahamellis$ python pywhi
Who's coming to the party? Tom
Who's coming to the party? Dick
Who's coming to the party? Harry
Who's coming to the party?
Data entry complete
Having a party for ['Tom', 'Dick', 'Harry']
program exiting
earth-wind-and-fire:~/feb05 grahamellis$ python pywhi
Who's coming to the party? Lisa
Who's coming to the party? Leah
Who's coming to the party? parents
Party plans abandoned
program exiting
earth-wind-and-fire:~/feb05 grahamellis$


See also Python Programming course

Please note that articles in this section of our web site were current and correct to the best of our ability when published, but by the nature of our business may go out of date quite quickly. The quoting of a price, contract term or any other information in this area of our website is NOT an offer to supply now on those terms - please check back via our main web site

Related Material

Python - Conditionals and Loops
  [299] - ()
  [353] - ()
  [657] - ()
  [668] - ()
  [788] - ()
  [835] - ()
  [909] - ()
  [1201] - ()
  [1477] - ()
  [1661] - ()
  [1696] - ()
  [2778] - ()
  [2899] - ()
  [3083] - ()
  [3200] - ()
  [3397] - ()
  [3439] - ()
  [3558] - ()
  [3620] - ()
  [3762] - ()
  [3895] - ()
  [4092] - ()
  [4210] - ()
  [4322] - ()
  [4323] - ()
  [4402] - ()
  [4541] - ()
  [4545] - ()
  [4713] - ()
  [4723] - ()

resource index - Python
Solutions centre home page

You'll find shorter technical items at The Horse's Mouth and delegate's questions answered at the Opentalk forum.

At Well House Consultants, we provide training courses on subjects such as Ruby, Lua, Perl, Python, Linux, C, C++, Tcl/Tk, Tomcat, PHP and MySQL. We're asked (and answer) many questions, and answers to those which are of general interest are published in this area of our site.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/solutions/python-t ... cture.html • PAGE BUILT: Wed Mar 28 07:47:11 2012 • BUILD SYSTEM: wizard