0

I was being told that recursive calling should be avoided and so I tried to use the return to end the function and call back the main function. However, it does not work perfectly as I thought (the print statement in the main function is not being executed), may I know which part of the code is causing the issue or is there any other approach to deal with recursive callings?

def view():
     print("View page")
     while True:
          view_option = input("Please enter 'back': ")
          if view_option == '':
               continue
          elif view_option == "back":
               return
               # being told to not use main() 

def main():
     print("Main page")
     while True:
          main_option = input("Please enter 'view': ")
          if main_option == "view":
               view()
          else:
               continue

main()

OUTPUT:

Main page
Please enter 'view': view
View page
Please enter 'back': back
Please enter 'view': 

EXPECTING OUTPUT:

Main page
Please enter 'view': view
View page
Please enter 'back': back
Main page
Please enter 'view': 
2
  • 2
    If you want to print "Main page" repeatedly then the print statement should be inside your loop. Commented Jul 11, 2021 at 8:48
  • 1
    Some of this code doesn't make sense. For instance, in main, the else: continue serves no purpose, right? What would happen if you deleted it? Nothing would change. It would continue the loop anyway. And in view, both break and return have the same effect inside the loop, right? So the elif serves no purpose. Commented Jul 11, 2021 at 8:50

1 Answer 1

1

Add the print statement inside of the while loop, like this:

def main():
    while True:
        print("Main page")
Sign up to request clarification or add additional context in comments.

Comments

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.