[Tutor] try except continue

Alan G alan.gauld at freenet.co.uk
Tue Aug 2 00:57:50 CEST 2005


---------- code here ---------
def f():
  if error:
    raise ValueError
  else:
    print "I'm in f()"

def g():
  print "I'm in g()"


def h():
  error = True
  print "error is: ", error
  n = 5
  while n:
    print "n is: ", n
    n -= 1
    try:
      f()
      g()
    except ValueError:
        error = False
        continue

h()

----------------------------------

The above fails because error is a local variable in h() so is not 
visible
in f(). If we pass error as an argument to f it works as expected:

$ python testry.py
error is:  True
n is:  5
n is:  4
I'm in f()
I'm in g()
n is:  3
I'm in f()
I'm in g()
n is:  2
I'm in f()
I'm in g()
n is:  1
I'm in f()
I'm in g()

It sounds like you are still struggling with this concept however.
Is there anything specificly that you don;t feel comfortable with?

Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld




More information about the Tutor mailing list