Re: Newbie Nested Function Problem

From: Brian Samek (zensunni_at_rochester.rr.com)
Date: 01/25/04


Date: Sun, 25 Jan 2004 03:47:17 GMT


> 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



Relevant Pages

  • Re: WARNING: Industry is Blogging These NewsGroups to Maintain Their Monopolies
    ... sure what you are exactly saying", you lost it a bit after that. ... > In other words it is okay to be a shill for the likes of Mercola, Blaylock, ... it is okay not to be a smug idiot. ... [Though frankly, health choices are finally personal, and ...
    (misc.health.alternative)
  • Re: Civil war in Israel - NOT
    ... I might agree with what you are saying. ... Does the fact that Jews are doing it make it okay? ... I just read there was a terrorist attack north of Jerusalem with ...
    (soc.culture.jewish.moderated)
  • Re: Goodbye!
    ... He's not saying it's okay, he's saying I'm grandstanding by objecting. ... of the use of killfiles. ... But when I went and looked at it on google ...
    (misc.fitness.weights)
  • Re: Conservative Joke (14A)
    ... It's okay for anyone to wish anyone is dead, ... :causing Sylvia's loved ones a problem in real life by suggesting ... Are you now saying it's okay to cause peoples' loved ones problems ... No, you're not weaselling, you're just mean, a big fat meanypants. ...
    (misc.writing)
  • Re: Suddenly, Disney doesnt seem important anymore
    ... People are dead and many who've survived have lost everything. ... >>> Jan, if you're out there, let us know you're okay. ... >Power and Phone services - but most are saying Months. ... I keep hearing "New Orleans was spared the brunt of the storm". ...
    (rec.arts.disney.parks)