Newbie Nested Function Problem

Rich Krauter rmkrauter at yahoo.com
Sat Jan 24 22:25:59 EST 2004


Looks like you are setting the variable 'leave' to the user input, and
then you are calling the function leave(), but remember that 'leave' has
been set to some string.
So say you enter 'xqz', and expect it to restart the loop when you get
to the leave call --- well, what you are doing is trying to call the
function xqz().
Rich
On Sat, 2004-01-24 at 21:45, Brian Samek wrote:

> 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()
> 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20040124/44fab7ee/attachment.html>


More information about the Python-list mailing list