Newbie Nested Function Problem

Josiah Carlson jcarlson at
Sun Jan 25 07:38:41 CET 2004

Brian Samek wrote:

> Oh wow!  Thanks a lot - that was exactly the issue.  I changed the variable name and the program works perfectly now.  I didn't realize that a variable name in a program would have that effect.
> Brian
>   "Rich Krauter" <rmkrauter at> wrote in message news:mailman.753.1075001453.12720.python-list at
>   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: 

One thing you should REALLY change is the way you get the number.  For 
general inputs, you need to deal with the fact that people may give bad 

     number = int(raw_input('prompt> '))
except KeyboardInterrupt:
     #ctrl+c was pressed
     #they didn't enter a number

Will evaluate some things that are entered by the user.

Below is a non-recursive version of what you wrote, which is not limited 
by the recursion limit of Python, so technically the upper bound can be 
tossed.  It also uses a controlled infinite loop trick that I've found 
quite useful in a few projects.

  - Josiah

def countdown():
     while 1:
             number = int(raw_input("Please enter a number.\n> "))
             if 1 <= number < 500:
         except KeyboardInterrupt:
             return 0
     while number > 0:
         print number
         number -= 1
     while 1:
         leave = raw_input("Type 'y' to start over - type 'n' to exit. ")
         if leave == 'y':
             return 1
         elif leave == 'n':
             return 0
             print "Type either 'y' or 'n' please."

while countdown():

More information about the Python-list mailing list