One way out of the top level is to call sys.exit(1) -----Original Message----- From: Orri Ganel [mailto:[EMAIL PROTECTED] Sent: Friday, January 28, 2005 4:26 PM To: Gilbert Tsang; Tutor@python.org Subject: Re: [Tutor] Control flow
Gilbert Tsang wrote: > 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" > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor