473,395 Members | 2,423 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

infinite loop


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
4 2597

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
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

43
by: Gremlin | last post by:
If you are not familiar with the halting problem, I will not go into it in detail but it states that it is impossible to write a program that can tell if a loop is infinite or not. This is a...
5
by: mailpitches | last post by:
Hello, Is there any way to kill a Javascript infinite loop in Safari without force-quitting the browser? MP
13
by: Vector | last post by:
Is any infinite loop better than other? Is there any difference between there efficiency?
10
by: Steven Woody | last post by:
i have a program which always run dead after one or two days, i think somewhere a piece of the code is suspicious of involving into a infinite loop. but for some reason, it is very hard to debug....
44
by: James Watt | last post by:
can anyone tell me how to do an infinite loop in C/C++, please ? this is not a homework question .
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.