0

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()
aaa
  • 93
  • 1
  • 7
  • 1
    Just a note, recursion is not always bad, its a tool that you can use, and, if used properly, its a great tool. You can read this (https://stackoverflow.com/questions/41469031/is-recursion-a-bad-practice-in-general/41469486) for more. – qBen_Plays Jun 12 '20 at 00:45
  • 1
    I would say recursion is bad in this instance because it adds nothing and results in potentially unbounded stack growth. In practice, it would take a long time for the stack growth to become a problem, but it is a clear implementation flaw. If someone wants to play again, you should loop, not recurse. – Tom Karzes Jun 12 '20 at 00:52

1 Answers1

0

The only recursive function chain I saw was menu->play->menu

Broke chain by not having play call menu.

We stay in the menu loop by leaving bool_play equal to 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()
DarrylG
  • 16,732
  • 2
  • 17
  • 23