Newbie Nested Function Problem

Brian Samek zensunni at rochester.rr.com
Sun Jan 25 04:47:17 CET 2004


> Okay, ask_number doesn't do what you're saying.  It's asking for a number,
> placed into variable number, and if it gets something out of range, prints
> an error message.
>
> And then ends.  It doesn't either a) repeat until it gets a valid number
> or b) do anything with the number it gets, like pass it back to the caller
> in a return statement, i.e.:
>

It does repeat if it doesn't get a valid number.  The function calls itself
after printing an error mesage.  For some reason the original message
formatted itself differently when I pasted it into my mail program.  The
line just before the first else statement should be indented to the same
level as the line before it so it reads:

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:



>   return number
>
> > ask_number()
>
> Okay, you invoked ask_number, but didn't even try to get anything from it.
> Normally, this would read something like:
>
> number_in = asknumber()
>

What do you mean by I "didn't even try to get anything from it."  I get a
variable called "number" from it from which the countdown(number) function
counts down.

> >    else:
>
> Um.. it looks like you're inadvertably making this a deeply recursive
> call, probably something you want to stay away from until you have regular
> stuff down pat.

I don't understand what you're saying.  I designed the program as three
functions nested within each other.  Are you saying I should be designing it
differently?  I made it a series of nested functions because, for example, I
needed to use the "number" variable from ask_number before it was destroyed.
When a function ends, any variables it has created are destroyed.

Thanks,

Brian





More information about the Python-list mailing list