Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7551947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T10:37:56+00:00 2026-05-30T10:37:56+00:00

In my script I have four functions that work like this: def function_four(): #

  • 0

In my script I have four functions that work like this:

def function_four():
    # Does Stuff
    function_one()

def function_three():
    # Does Stuff
    function_two()

def function_one(): 
    usr_input = input("Options: '1') function_three | '2') Quit\nOption: ")
    if usr_input == '1':
        function_three()
    elif usr_input == '2':
        sys.exit()
    else:
        print("Did not recognise command. Try again.")
        function_one()

def function_two():
    usr_input = input("Options: '1') function_four | '2') function_three | '3') Quit\nOption: ")
    if usr_input == '1':
        function_four()
    elif usr_input == '2':
        function_three()
    elif usr_input == '3':
        sys.exit()
    else:
        print("Did not recognise command. Try again.")  
function_one()

I need to know if this will cause the problem I think it will: the functions never closing, causing lots of open functions (and, presumably, wasted memory and eventual slow-down) to appear, never to disappear until the user quits the script. If true, then this would most likely be bad practise and inadvisable, meaning that there must be an alternative?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T10:37:57+00:00Added an answer on May 30, 2026 at 10:37 am

    Whenever you have Python code that is:

    Recalling The Same Function So That If The User Does Not Chose One Of The Other Statements Then They Can Try Again Rather Than The Program To Stop Working,

    you are almost always better off replacing the recursive call with a loop. In this case the recursion is completely unnecessary, probably wastes resources and arguably makes the code harder to follow.

    edit: Now that you’ve posted the code, I’d suggest recasting it as a state machine. The following page provides a summary of Python modules that could be useful: link.

    Even without any additional modules, your code lends itself to a simple non-recursive rewrite:

    import sys
    
    def function_four():
        # Does Stuff
        return function_one
    
    def function_three():
        # Does Stuff
        return function_two
    
    def function_one():
        usr_input = input("Options: '1') function_three | '2') Quit\nOption: ")
        if usr_input == '1':
            return function_three
        elif usr_input == '2':
            return None
        else:
            print("Did not recognise command. Try again.")
            return function_one
    
    def function_two():
        usr_input = input("Options: '1') function_four | '2') function_three | '3') Quit\nOption: ")
        if usr_input == '1':
            return function_four
        elif usr_input == '2':
            return function_three
        elif usr_input == '3':
            return None
        else:
            print("Did not recognise command. Try again.")
            return function_two
    
    state = function_one
    while state is not None:
        state = state()
    

    Note that the functions no longer call each other. Instead, each of them returns the next function to call, and the top-level loop takes care of the calling.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a script that loops through a series of four (or less) characters
I have script like this : fullscript.sh if [ ! -f /opt/laks/linux-2.6.33.2.tar.bz2 ] then
I am writing a script and in my script I have this function: def
I have this mail script I have to run a few times. To start
This is the script I have written for gzipping content on my site, which
This is a simple script I have written to test command line argument handling:
I have a web-app that has a master mysql db and four slave dbs.
i have a script that calls a php file and gets an JSON object
I have a form that submits to a PHP script with Jquery and Ajax.
I will try to explain the problem that I have with this code. This

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.