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?
mainwhileoption == 1exceptclause will never be hit. Did you intend for theoption = int(input())line to be inside thetry? You know, you don't really need to incur the cost of exceptions there. You can sayif option == '1':and skip the conversion to integer.