[Tutor] How to exit program without sys.exit()

David david at abbottdavid.com
Thu Dec 18 00:33:00 CET 2008


Hi,
I make these silly programs to learn from examples I find on the list. I 
put a couple together just to practice. I have heard it is not a good 
idea to use sys.exit() but I can not figure out how to do it. Also any 
and all comments are welcome. Thanks

#!/usr/bin/python
import sys
_count = 0

def getinfo():
     answer = yesno("Enter y to continue, n to exit: y/n >> ")
     if answer == "y":
         getnumber()
     else:
         sys.exit()

def counter():
     global _count
     _count += 1
     return _count

def yesno(question):
     responce = None
     while responce not in ("y", "n"):
         responce = raw_input(question).lower()
     return responce

def getnumber():
     try:
         num = int(raw_input("Enter a number between 25 and 75: "))
         if 25 < num < 75:
             print "WOW you are smart ", sayit()
     except ValueError:
         print "Please Enter a number!", getnumber()


def sayit():
     print "Your total correct answers is", counter()
     again()


def again():
     onemore = raw_input("Again? y/n >> " )
     if onemore.lower() == "y":
         getnumber()
     elif onemore.lower() == "n":
         getinfo()
     else:
         sys.exit


def main():
     getinfo()

if __name__=="__main__":
     main()



-- 
Powered by Gentoo GNU/LINUX
http://www.linuxcrazy.com
pgp.mit.edu



More information about the Tutor mailing list