Reputation: 217
I have a program that I want to indefinitely run that displays a random 5 long alphanumeric string with time generated. I have a condition that checks the string to display that a substring was included in the generation. The problem is that I get the error:
RuntimeError: maximum recursion depth exceeded
I have the following:
import random,datetime
def gen():
global rand
rand=''.join(random.choice('0123456789abcdefghijklmnopqrstuvwxyz') for i in range(5))
return rand
def main(num):
print datetime.datetime.now(),'::',num
if 'xxx' in num:
print 'Generated string contains xxx! Continuing...'
main(gen())
else:
print datetime.datetime.now(),'::', num
'xxx not in string.'
main(gen())
main(gen())
How can I go about converting this or rectifying this issue? Thank you
Upvotes: 0
Views: 155
Reputation: 49318
Simply use an infinite loop, and call gen()
from within the loop rather than passing it as an argument to main
:
def main():
while 1:
num = gen()
print datetime.datetime.now(),'::',num
if 'xxx' in num:
print 'Generated string contains xxx! Continuing...'
else:
print datetime.datetime.now(),'::', num
'xxx not in string.'
main()
Upvotes: 2