簡體   English   中英

遞歸迭代? 超過最大遞歸深度

[英]Recursion to Iteration? Max recursion depth exceeded

我有一個要無限期運行的程序,該程序顯示隨時間變化的隨機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())

我該如何進行轉換或糾正此問題? 謝謝

只需使用無限循環,然后從循環內部調用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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM