David wrote:
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

Others have made valuable comments. I add:

It is better to use a while loop rather than recursive function calls. The logic is a lot easier to follow, and you won't run into the recursive depth limit. Also restrict try blocks to just the statement likely to raise the error you are expecting.

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

def getnumber():
   num = 0
   while True:
       try:
           num = int(raw_input("Enter a number between 25 and 75: "))
       except ValueError:
           print "Please Enter a number!"
       if 25 < num < 75:
           print "WOW you are smart ", sayit()
break There are a lot more recursive calls that I will not try to "fix". I hope you get the idea.

In fact I'd start out with no functions at all - just one program in a loop. See what you can accomplish that way, then add functions only as needed.

#!/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()





--
Bob Gailer
Chapel Hill NC 919-636-4239

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to