# Newbie Nested Function Problem

Josiah Carlson jcarlson at nospam.uci.edu
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 yahoo.com> wrote in message news:mailman.753.1075001453.12720.python-list at python.org...
>   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
input.

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

input(<prompt>)
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:
try:
number = int(raw_input("Please enter a number.\n> "))
if 1 <= number < 500:
break
except KeyboardInterrupt:
return 0
except:
pass
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
else:
print "Type either 'y' or 'n' please."

while countdown():
pass

```