[Tutor] Control flow
Gilbert Tsang
gtsang at lnxw.com
Fri Jan 28 21:02:51 CET 2005
Hi there, I have this logic that I cannot wrap my mind it:
def go_jogging():
# go out and jog
return
if ( bad_weather =='y' ):
# ask user only if weather is bad.
b = input ( "Weather is really bad, still go out to jog?[y/n]" )
if b == 'y':
go_jogging()
else:
# program should exit now
else:
go_jogging()
####################################################
I can't get the program to stop processing further in the middle
(apparently neither exit nor goto-label exist in Python, sorry for the
C++ mindset) so I used exception to achieve what I want. I know in that
example you could probably manipulate the logic so that program ends at
the bottom of the if-tree. My question is then how to exit in the middle
of a if-then-else tree? Thanks, Gilbert.
try:
if ( bad_weather =='y' ):
b = input ( "Weather is really bad, still go out to jog?[y/n]" )
if b == 'y':
go_jogging()
else:
raise Exception( "quit" )
else:
go_jogging()
except Exception, inst:
print "Program exits now"
More information about the Tutor
mailing list