[英]How can the code below be done without recursions?
I heard that recursions are bad and I am having trouble finding a solution on having code that is pretty much the equivalent of this but once again, with no recursions.我听说递归很糟糕,我很难找到一个解决方案,让代码几乎等同于此,但又一次,没有递归。 Can someone help me come up with code that does the same thing except without any functions calling themselves?
有人可以帮我想出做同样事情的代码,除了没有任何函数调用自己吗?
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()
The only recursive function chain I saw was menu->play->menu我看到的唯一递归 function 链是 menu->play->menu
Broke chain by not having play call menu.由于没有播放呼叫菜单而中断了链条。
We stay in the menu loop by leaving bool_play equal to False我们通过让 bool_play 等于 False 留在菜单循环中
import os
from time import sleep
def cont():
input('- Press enter to continue -\n')
def cls():
os.system('cls') # For Windows
os.system('clear') # For Linux/OS X
def intro():
print('Welcome to No Escape')
print('- By Anonymous -')
sleep(2)
cls()
def play():
print('play')
sleep(1)
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()
play()
break
elif possible_menu_answers.index(a) == 1:
cls()
menu_help()
cont()
break
elif possible_menu_answers.index(a) == 2:
cls()
settings()
cont()
break
elif possible_menu_answers.index(a) == 3:
#quit() # don't need to end process
bool_play = True # we exit while loop by setting
# bool_play = True
break
else:
print('Invalid')
sleep(0.5)
#intro()
menu()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.