By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,173 Members | 796 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,173 IT Pros & Developers. It's quick & easy.

infinite loop

P: n/a

Hi all,

I have a question with some code I'm writting:
def main():

if option == 1:

function_a()

elif option == 2:

function_b()

else:

raise 'option has to be either 1 or 2'

if iteration == True:

main()

def function_a():

print 'hello from function a'

return None

def function_b():

print 'hello from function b'

return None

iteration = True

option = 1

main()
I want an infinite loop, but after some iterations (996) it breaks:
[alopez@dhcp-222 tmp]$ python test.py
hello from function a
hello from function a
hello from function a
..
..
..
hello from function a
hello from function a
Traceback (most recent call last):
File "test.py", line 35, in ?
main()
File "test.py", line 17, in main
main()
File "test.py", line 17, in main

..
..
..
..
File "test.py", line 17, in main
main()
File "test.py", line 17, in main
main()
File "test.py", line 5, in main
function_a()
RuntimeError: maximum recursion depth exceeded
I don't understand it. Why am I not allowed to iterate infinitely? Something about the functions? What should I do for having an infinite loop?

Thanks in advance for your help,

Adrián.
Sep 6 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a

LOPEZ GARCIA DE LOMANA, ADRIAN wrote:
Hi all,

I have a question with some code I'm writting:
def main():

if option == 1:

function_a()

elif option == 2:

function_b()

else:

raise 'option has to be either 1 or 2'

if iteration == True:

main()

def function_a():

print 'hello from function a'

return None

def function_b():

print 'hello from function b'

return None

iteration = True

option = 1

main()
I want an infinite loop, but after some iterations (996) it breaks:
[alopez@dhcp-222 tmp]$ python test.py
hello from function a
hello from function a
hello from function a
.
.
.
hello from function a
hello from function a
Traceback (most recent call last):
File "test.py", line 35, in ?
main()
File "test.py", line 17, in main
main()
File "test.py", line 17, in main

.
.
.
.
File "test.py", line 17, in main
main()
File "test.py", line 17, in main
main()
File "test.py", line 5, in main
function_a()
RuntimeError: maximum recursion depth exceeded
I don't understand it. Why am I not allowed to iterate infinitely? Something about the functions? What should I do for having an infinite loop?

Thanks in advance for your help,

Adrián.


You've written a recursive function-you're not iterating. The recursion
limit is there to keep you from making something which will do
something bad, like recurse cyclically.

Sep 6 '05 #2

P: n/a
LOPEZ GARCIA DE LOMANA, ADRIAN wrote:
Hi all,

I have a question with some code I'm writting:
def main():
if option == 1:
function_a()
elif option == 2:
function_b()
else:
raise 'option has to be either 1 or 2'
if iteration == True:
main()
... I want an infinite loop, but after some iterations (996) it breaks:
... RuntimeError: maximum recursion depth exceeded
I don't understand it. Why am I not allowed to iterate infinitely?
Something about the functions? What should I do for having an infinite loop?


You are asking in your code for infinite recursive regress.
Eventually the stack overflows.

An infinite loop would look like:

def main():
if option == 1:
function_a()
elif option == 2:
function_b()
else:
raise 'option has to be either 1 or 2'
while iteration:
if option == 1:
function_a()
elif option == 2:
function_b()
else:
raise 'option has to be either 1 or 2'

Which you might want to rewrite as:
def main():
choices = {1: function_a, 2:function_b}
choices[option]()
while iteration:
choices[option]()

--Scott David Daniels
Sc***********@Acm.Org
Sep 6 '05 #3

P: n/a
Devan L wrote:
LOPEZ GARCIA DE LOMANA, ADRIAN wrote:
Hi all,

I have a question with some code I'm writting:
def main():

if option == 1:

function_a()

elif option == 2:

function_b()

else:

raise 'option has to be either 1 or 2'

if iteration == True:

main()

def function_a():

print 'hello from function a'

return None

def function_b():

print 'hello from function b'

return None

iteration = True

option = 1

main()
I want an infinite loop, but after some iterations (996) it breaks:
[alopez@dhcp-222 tmp]$ python test.py
hello from function a
hello from function a
hello from function a
.
.
.
hello from function a
hello from function a
Traceback (most recent call last):
File "test.py", line 35, in ?
main()
File "test.py", line 17, in main
main()
File "test.py", line 17, in main

.
.
.
.
File "test.py", line 17, in main
main()
File "test.py", line 17, in main
main()
File "test.py", line 5, in main
function_a()
RuntimeError: maximum recursion depth exceeded
I don't understand it. Why am I not allowed to iterate infinitely? Something about the functions? What should I do for having an infinite loop?

Thanks in advance for your help,

Adrián.


You've written a recursive function-you're not iterating. The recursion
limit is there to keep you from making something which will do
something bad, like recurse cyclically.


What you need is probably this...

def main():
while iteration:
if option == 1:
function_a()
elif option == 2:
function_b()
else:
raise 'option has to be either 1 or 2'

def function_a():
print 'hello from function a'

def function_b():
print 'hello from function b'

iteration = True
option = 1
main()

As a side note, note that you don't really need to return a None.

Sep 6 '05 #4

P: n/a
"LOPEZ GARCIA DE LOMANA, ADRIAN" <al****@imim.es> writes:
Hi all,

I have a question with some code I'm writting:
def main():
if option == 1:
function_a()
elif option == 2:
function_b()
else:
raise 'option has to be either 1 or 2'
if iteration == True:
main() [...] I want an infinite loop, but after some iterations (996) it breaks:


Since no one else mentioend it: this is only iteration in languages
which mandate tail recursion elimination. Languages that don't do that
are free to do the recursion, which will eventually run you out of
stack. Python is in the latter category, and that's what you ran into.

Thinking about iteration this way is elegant - but it doesn't work
everywhere. Sorry.

<mike
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Sep 7 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.