【问题标题】:How can the code below be done without recursions?下面的代码如何在没有递归的情况下完成?
【发布时间】: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


【解决方案1】:

我看到的唯一递归函数链是menu->play->menu

由于没有播放呼叫菜单而中断链。

我们通过让 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()

【讨论】:

    猜你喜欢
    • 2023-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-04
    • 2017-05-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多