0
while True:
    print("Menu:")
    print("1. Add Task")
    print("2. Display Tasks")
    print("3. Update Task Status")
    print("4. Exit")

    choice = input("Enter your choice: ")

    if choice == "1":
        print("Please enter 'exit' if you want to back to main menu")
        title = input("Enter task title: ")
        if title != " Back" or " back" or " exit":
            description = input("Enter task description: ")
            due_date = input("Enter task due date: ")
            status = input("Enter task status: ")
        elif title == " exit":
            continue

in this code, I am trying to tell it if it says the following: we will return to the main menu. If he says anything else, the loop will continue. Yet it is doing the reverse for me. If I say the keyword it continues the loop. If I say something different, it says the word is not correct and ends the code.

I tried adding continue as it's supposed to continue to the next iteration of the loop. I thought it would go back. I'm just confused.

1 Answers1

0

Yes, that is precisely what continue does: it stops the current iteration and continues directly to the next one. To quote the doc:

The continue statement rejects all the remaining statements in the current iteration of the loop and moves the control back to the top of the loop.

Maybe you are looking for the break keyword, which exits the loop and proceeds with the next instruction:

The break statement in Python terminates the current loop and resumes execution at the next statement.

while True:
    print("Menu:")
    print("1. Add Task")
    print("2. Display Tasks")
    print("3. Update Task Status")
    print("4. Exit")

    choice = input("Enter your choice: ")

    if choice == "1":
        print("Please enter 'exit' if you want to back to main menu")
        title = input("Enter task title: ")
        if title != " Back" or " back" or " exit":
            description = input("Enter task description: ")
            due_date = input("Enter task due date: ")
            status = input("Enter task status: ")
        elif title == " exit":
            break

Here is some documentation about these keywords and their usage.