python - 如何更改递归函数
问题描述
如何更改函数以使其不再是递归的?我认为内存缓冲区正在被填充。
代码:
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()
解决方案
这一点都不难。只是没有函数调用本身。如果您希望它永远循环,请明确说明。
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
推荐阅读
- asp.net-mvc - 如何将查询字符串中的对象传递给控制器?
- python - Canopy 环境中的 Python 错误“'float' 对象不能被解释为索引”
- rust - 如何检查远程 UDP 端口是否打开?
- coldfusion - 分割后的ColdFusion地板功能
- gmail - 如何以编程方式检查 Gmail 电子邮件地址是否存在
- http - Why is my port forwarding not working?
- android - Not able to access localhost on android device on the same network
- drupal-8 - Day and month name translation in Drupal8
- sql - 如何将多个sql行连接成一行
- ios - 如何构建一个按钮来单独控制计时器并在标签中显示倒计时?