首页 > 解决方案 > 如何更改递归函数

问题描述

如何更改函数以使其不再是递归的?我认为内存缓冲区正在被填充。

代码:

def start_play():
    start = driver.find_element_by_xpath('/html/body/div[27]/div[2]/div[3]/div[1]/div[1]/span')
    start = start.text
    start = re.findall(r'\d+', start)

    if len(start) == 0:
        # t.sleep(2)
        return start_play()
    else:
        if start[0] == '3' or start[0] == '4':
            comparison()
        else:
            return start_play()
        
def check_win_or_lose():
    start = driver.find_element_by_xpath('/html/body/div[27]/div[2]/div[3]/div[1]/div[1]/span')
    start = start.text
    start = re.findall(r'\d+', start)

    if len(start) == 0:
        return check_win_or_lose()
    else:
        if start[0] == '22' or start[0] == '23':
            check()
            last_bet.clear()
        else:
            return check_win_or_lose()

标签: pythonpython-3.xseleniumrecursion

解决方案


这一点都不难。只是没有函数调用本身。如果您希望它永远循环,请明确说明。

def wrong():
   # do things here
   return wrong()

def right():
    while True:
        # do things here

如果你想退出while True:某个地方# do things here,你可以用break.

def wrong_too():
   # do things here
   if something:
       # do nothing, or something else
   else:
       return wrong_too()

def right_too():
    while True:
        # do things here
        if something:
            break

在更复杂的情况下,可能会使用状态变量。

def right_also():
    done = False
    while not done:
        # do things here
        while complex_stuff():
            for loop in values():
                if something_particular():
                    # We want to escape down at the end of the while True
                    done = True

推荐阅读