infinite loop

  • Thread starter LOPEZ GARCIA DE LOMANA, ADRIAN
  • Start date
L

LOPEZ GARCIA DE LOMANA, ADRIAN

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.
 
D

Devan L

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.
 
S

Scott David Daniels

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
(e-mail address removed)
 
J

James

Devan said:
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.
 
M

Mike Meyer

LOPEZ GARCIA DE LOMANA said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top