Newbie Nested Function Problem

Rich Krauter rmkrauter at yahoo.com
Sat Jan 24 23:18:21 EST 2004


I should have suggested a 'fix' before: change all the "leave"s which
are referring to, or defining the function "leave" to "leaver" or
something like that. Keep all program's references to the variable
"leave" unchanged. Then your program should work.

To clarify my previous reply, your problem is this:

leave = 'xqz'
leave()  

The leave() call is actually trying to call a string object, which is
not callable. 
So I wasn't quite right in what I told you in the previous post - the
leave() call is not trying to call the function xqz(), its trying to
call the string xqz.

Rich

On Sat, 2004-01-24 at 22:47, Brian Samek wrote:

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


More information about the Python-list mailing list