How to execute certain part of the code in a While-Loop?

Question:

This is a modified working section of my code just to make it simple.

The idea is that the user enters from the main menu to a submenu, and there is asked to press ENTER to continue or ESC to return to the main menu.
When the user presses ENTER he/she should see QUESTION #1 and be able to perform some stuff, once that is done, another question, QUESTION #2 should pop up and ask if the user wants to perform stuff again or not.
If the user decides yes, then the loop continues and returns to QUESTION #1, otherwise, the loop breaks and returns to the main menu.
Everything is fine til that point.

However, if the user makes a mistake in QUESTION #2 and instead of inserting ‘y’ or ‘n’ inserts something else, i.e ‘nm’ or ‘yt’, etc. the loop at the moment breaks and returns them to the main menu. If I use Continue it will restart the loop and the user is obligated to perform stuff again.

I want to be able to bypass QUESTION #1 when the user inputs something wrong that is not ‘y’ or ‘n’ and asks me again to retry directly from QUESTION #2

import msvcrt
import time

#FUNCTIONS
def menu_options():
    print("MAIN MENU Options")

def option_sub01():
    print("SUB MENU Options, press ENTER to continue or ESC twice to exit")

def option01():   
    while True:
        #QUESTION #1 
        print("Question #1")

        #do stuff#
        print("doing stuff...")
        time.sleep(3)
        
        #QUESTION #2
        check = input("""Question #2, 
        press 'y' to continue loop and do stuff again, or 'n' to break loop and return to main menu.""")
        if check == 'y':                                              
            continue
        elif check == 'n':
            break
        else:
            print("Wrong Input.")
            break #breaks the loop and returns to main menu if user inputs any other letter that is not Y or N.
           #continue continues the loop but instead of asking the user to try again, it restart the whole loop and the user is obligated to answer QUESTION #1.

#START
while True:
    menu_options()
    
    o = input("Choose an option (only option 1 works): ")

    if o == '1':
        option_sub01()
        if ord(msvcrt.getch()) == 13:
            option01()
            print()
        elif ord(msvcrt.getch()) == 27: #For some reason I must press ESC key twice for it to work.
            continue
        else:
            print("Wrong input.")

Bonus issue: I do not understand why Python makes me press the ESC key twice for it to work, and the key ENTER works at once.

Thanks in advance.

Asked By: Strider

||

Answers:

I’ve added a variable called reset which, under the right circumstances, would allow the user to break out of both loops. Here is some code you can use to implement it:

import time


def task():
    while True:
        reset = False
        
        print('QUESTION #1')
        # Do some stuff
        time.sleep(3)
        
        while True:
            check = input("""Question #2,
                  press 'y' to continue loop and do stuff again, or 'n' to break loop and return to main menu.""").lower()

            if check == 'y':
                break
            elif check == 'n':
                reset = True
                break
            else:
                print('Invalid input, please try again!')
                time.sleep(3)
                continue
            
        if reset:
            break


task()

Hope this helps.

Answered By: BappoHotel
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.