How can I "skip a section" of a while loop?

Or maybe that’s not the proper way to approach this issue…


def main():
    active = True
    while(active):
        global thread_viewed
        thread_viewed = False
        [print("Welcome to the (name) Forum!\n" +
          "To make a post, type '/post'.\n" +
          "To quit, type '/quit'.\n" +
          "To view a post, type '/view post_number'.\n" +
          "Otherwise, press any other key.\n")
        display_posts()]
        command = input()
        if command == "/quit":
            active = False
        elif command == "/post":
            post()
        elif command.startswith("/view"):
            view(command)
        else:
            pass

For this section of code, I have a couple functions that run (e.g view and post). When these functions finish running, the loop starts from the beginning and the menu is displayed again. This is good.

The issue is with my else statement. While this behaviour is okay for the functions, it creates an issue where if the user types any other key, the menu prints again even if they don’t need to see it again. I’d like to know if there’s a way to skip the bits between the square brackets for the else clause, or if there’s any other way to skip the menu for displaying again if a user does not type “/quit”, “/post”, or “/view”.

Thanks

Is this what you want? It will still display the banner if the user types /post or /view/…, if that’s what you want.

def main():
    display_banner = True
    active = True
    while(active):
        global thread_viewed
        thread_viewed = False
        if display_banner:
            print("Welcome to the (name) Forum!\n" +
                  "To make a post, type '/post'.\n" +
                  "To quit, type '/quit'.\n" +
                  "To view a post, type '/view post_number'.\n" +
                  "Otherwise, press any other key.\n")
            display_posts()
        command = input()
        if command == "/quit":
            active = False
        elif command == "/post":
            post()
            display_banner = True
        elif command.startswith("/view"):
            view(command)
            display_banner = True
        else:
            display_banner = False

yeah, thank you!

Maybe this version is better:

def main():
    display_banner = True
    active = True
    while(active):
        global thread_viewed
        thread_viewed = False
        if display_banner:
            print("Welcome to the (name) Forum!\n" +
          "To make a post, type '/post'.\n" +
          "To quit, type '/quit'.\n" +
          "To view a post, type '/view post_number'.\n" +
          "Otherwise, press any other key.\n")
            display_posts()
            display_banner = False
        command = input()
        if command == "/quit":
            active = False
        elif command == "/post":
            post()
        elif command.startswith("/view"):
            view(command)
        else:
            # Only affects the next iteration
            display_banner = True

I fixed an oversight in my code, you might want to update it.

Just a note:

  1. continue goes to the top of the loop.
  2. break exits a loop completely.