Newbie Nested Function Problem

Brian Samek zensunni at rochester.rr.com
Sat Jan 24 21:45:02 EST 2004


I began learning python a few days ago and just wrote the following program.
I have nested three functions together.  For some reason, the leave function
works fine when 'y' or 'n' are inputted, but it does not restart itself when
other things are inputted.  Why is this?

Thanks,

Brian Samek

# Brian Samek
# Created 01/24/2004

# This program counts down from a user-defined integer less than or equal
# to 500.

print "This program counts down from any number less than or equal to 500"
print

# ask_number gets the number from the user and then puts it into countdown

def ask_number():
    number = input("Please enter a number.\n")
    if number > 500 or number - int(number) != 0 or number < 1:
        print "Input positive integers less then 501 only, please."
 ask_number()
    else:
# countdown does the actual counting down until 0
        def countdown (number):
            if number != 0:
                print number
                number = number - 1
                countdown (number)
            else:
# leave asks the user if he wishes to exit
                def leave():
                    leave = raw_input ("Type 'y' to start over - type 'n' to
exit. ")
                    if leave == "y":
                        ask_number()
                    elif leave == "n":
                        return
                    else:
                        print "Type either 'y' or 'n' please."
                        leave()
                leave()
        countdown (number)
ask_number()





More information about the Python-list mailing list