【发布时间】:2020-06-12 00:41:44
【问题描述】:
我听说递归很糟糕,我很难找到一个解决方案,让代码几乎等同于此,但又一次,没有递归。有人可以帮我想出做同样事情的代码,除了没有任何函数调用自己吗?
import os
from time import sleep
def cont():
input('- Press enter to continue -\n')
def cls():
os.system('cls')
def intro():
print('Welcome to No Escape')
print('- By Anonymous -')
sleep(2)
cls()
def play():
print('play')
sleep(1)
bool_play = False
menu()
def menu_help():
print('help')
def settings():
print('sett')
possible_menu_answers = ('play', 'help', 'settings', 'quit')
def menu():
bool_play = False
while not bool_play:
cls()
print('Xx No Escape xX\n')
print('.:Play:.')
print('.:Help:.')
print('.:Settings:.')
print('.:Quit:.')
menu_option = input('> ')
if menu_option in possible_menu_answers:
for a in possible_menu_answers:
if menu_option == a:
if possible_menu_answers.index(a) == 0:
cls()
bool_play = True
play()
elif possible_menu_answers.index(a) == 1:
cls()
menu_help()
cont()
break
elif possible_menu_answers.index(a) == 2:
cls()
settings()
cont()
elif possible_menu_answers.index(a) == 3:
quit()
else:
print('Invalid')
sleep(0.5)
#intro()
menu()
【问题讨论】:
-
请注意,递归并不总是坏事,它是一个可以使用的工具,如果使用得当,它是一个很棒的工具。您可以阅读此 (stackoverflow.com/questions/41469031/…) 了解更多信息。
-
我会说在这种情况下递归是不好的,因为它没有添加任何内容并导致潜在的无限堆栈增长。在实践中,堆栈增长需要很长时间才能成为问题,但这是一个明显的实现缺陷。如果有人想再玩一次,你应该循环,而不是递归。
标签: python