[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
    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
    except ValueError:
        error = False



The above fails because error is a local variable in h() so is not 
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

More information about the Tutor mailing list