简体   繁体   English

下面的代码如何在没有递归的情况下完成?

[英]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.

相关问题 不用循环或递归怎么解决,百思不得其解? - how to solve without loop or recursions, puzzling? 如下所示,如何使用opencv保存图像? - How to save image using opencv after done this code as shown below? 我已经完成了没有.kv 文件的 python kivy 代码。 我已经完成了下面的屏幕管理器,第一个屏幕的小部件正在显示,但不是第二个 - i have done a python kivy code without .kv file. I have done with screenmanager below,the first screen's widgets are displaying but not second pypy如何处理递归? - How does pypy handle recursions? 如何在 python 中没有 memory 错误的情况下进行数十亿次计算? - How can a billions of computations be done without having memory error in python? 如何加速下面的代码? 实现没有中心元素的 maxpool - How to accelerate the code below? Implementing maxpool without center element 如何在下面的代码中不使用href找到url - How to find url without using href in the below code 我想在 Ruby 脚本中写一个 python 的代码,怎么实现? - I want to write a python code in Ruby script, how can that be done? 如何将下面的 for 循环代码更改为更快的方式,例如没有循环? - How to change the below for loop code to a faster way,for example without a loop? 如何在不加入两个数据帧的情况下在 Python 中对下面的代码进行矢量化 - How to vectorize this code below in Python without joining two dataframes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM