Newbie Nested Function Problem
Terry Carroll
carroll at tjc.com
Sat Jan 24 22:17:06 EST 2004
On Sun, 25 Jan 2004 02:45:02 GMT, "Brian Samek"
<zensunni at rochester.rr.com> 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?
I haven't tried running this, but...
># 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."
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.:
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()
> 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.
More information about the Python-list
mailing list