Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Python and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Lua, etc
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"
    if said == "":
        print "Data entry complete"
        running = 0
    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
  [4723] Conditional operators in Python - (2016-11-05)
  [4713] Equality (in Python) - (2016-10-30)
  [4545] Method, Class, Module, Package - how to they relate in Python? - (2015-10-17)
  [4541] Setting up and tearing down with the Python with keyword - (2015-10-16)
  [4402] Finding sum, minimum, maximum and average in Python (and Ruby) - (2015-01-19)
  [4323] Learning to program - Loop statements such as while - (2014-11-22)
  [4322] Learning to Program - the conditional statement (if) - (2014-11-21)
  [4210] If elif elif elif - multiway selection in Python - (2013-11-16)
  [4092] Identity in Python - (2013-05-17)
  [3895] Flowchart to program - learning to program with Well House - (2012-10-14)
  [3762] Learning to program - the if statement. Python. - (2012-06-12)
  [3620] Finding the total, average, minimum and maximum in a program - (2012-02-22)
  [3558] Python or Lua - which should I use / learn? - (2011-12-21)
  [3439] Python for loops - applying a temporary second name to the same object - (2011-09-14)
  [3397] Does a for loop evaluate its end condition once, or on every iteration? - (2011-08-18)
  [3200] How a for loop works Java, Perl and other languages - (2011-03-12)
  [3083] Python - fresh examples from recent courses - (2010-12-11)
  [2899] Groupsave tickets - 3 or 4 train tickets for the price of 2 - (2010-08-02)
  [2778] Learning to program in Python 2 ... and / or in Python 3 - (2010-05-24)
  [1696] Saying NOT in Perl, PHP, Python, Lua ... - (2008-07-04)
  [1661] Equality, sameness and identity - Python - (2008-05-31)
  [1477] Decisions - small ones, or big ones? - (2007-12-18)
  [1201] No switch in Python - (2007-05-23)
  [909] Python is like a narrowboat - (2006-10-30)
  [835] Python - when to use the in operator - (2006-08-16)
  [788] New - Conditional expressions in Python 2.5 - (2006-07-01)
  [668] Python - block insets help with documentation - (2006-04-04)
  [657] The ternary operator in Python - (2006-03-25)
  [353] Wimbledon Neck - (2005-06-20)
  [299] What - no switch or case statement? - (2005-05-03)

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., 2019: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01225 708225 • FAX: 01225 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