我有一个程序,我想无限期地运行,显示一个随机的5长字母数字字符串,生成时间。我有一个条件,检查字符串以显示生成中包含子字符串。问题是我得到了错误:
RuntimeError: maximum recursion depth exceeded
我有以下内容:
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())
我如何进行转换或纠正此问题?谢谢
答案 0 :(得分:2)
只需使用无限循环,并在循环中调用gen()
,而不是将其作为参数传递给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()