1

I'm starting to learn Python. Right now I'm creating a CLI that allows to create, view or delete contacts stored in a SQLite 3 DB. The problem is that everytime I finish a task I call the main function again so a user can do other things. The code looks like this:

def main(self):
    print("What operation would you like to perform: Display contacts (1), add a new one (2), or remove one  (3)?")
    option = int(input())
    try:
        if option == 1:
            self.display()
            self.main()
        elif option == 2:
            self.new()
            self.main()
        elif option == 3:
            self.delete()
            self.main()
    except TypeError:
        print("Please introduce a valid option")
        sys.exit()

I'm pretty sure that successive calls of a function decrease its performance and I think there is a limit of how many times you can call a recursive function, so how should I call the main method again?

2
  • 4
    Use a loop to repeat all the contents of main while option == 1 Commented Mar 31, 2021 at 18:28
  • 1
    Your except clause will never be hit. Did you intend for the option = int(input()) line to be inside the try? You know, you don't really need to incur the cost of exceptions there. You can say if option == '1': and skip the conversion to integer. Commented Mar 31, 2021 at 18:33

3 Answers 3

1

You're right about recursive calls. They can be good, but iteration is often better than recursion.

There's a lot that you can do to do infinite programm, but the easiest way, for cli, is to put all your function in a loop.

This is kinda ugly, but that should work without you having to call for main() everytime.

def main(self):
    again=True
    while again:
        print("What operation would you like to perform: Quit(0) Display contacts (1), add a new one (2), or remove one  (3)?")
        option = int(input())
        if option == 0:
            again=False
        if option == 1:
            self.display()
            self.main()
        elif option == 2:
            self.new()
            self.main()
        elif option == 3:
            self.delete()
            self.main()
        else:
            print("Please introduce a valid option")

With that said, you still have to call main()

Sign up to request clarification or add additional context in comments.

3 Comments

continue is a keyword and can't be used as variable name or expression.
continue=True is a syntax error, continue is a keyword
You still have the continue keyword in your code, at continue=False
-1

You can use argument libraries like getopt or argparse

The main goal here is manage the options inside a loop and call it all at once. Ex:

$ python3 ./script.py -123

Comments

-2
In python there is no such thing as "main function" or "main method". If you want, you can read about constructors, call operations and etc. So, if I understood correctly - you want to recursive call "main" method.

You can count occurrences in this method like this: def main(self, count= <your number>): And after this, if you want to call this recursive, create loop or just sum "count":

<some code here>
if count == <your number>:
    return
else:
    count += 5
self.main(count)

1 Comment

I didn't downvote, but it sounds like the OP is asking exactly the opposite, i.e. how not to use recursion here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.