'how to use keyboard module in python. when user hit 1 then executing second loop where there ara two choices 0 or 1. when user hit 1 it don't work

here this simple program is for to understand how to manipulate with keyboard module. problem is that when user hit number 1 loop moving to the second loop, where there are two options for user hit 1 or hit 0. when user hit 1 second loop, program should should print 'second loop canceled', stop executing second while loop and should return to the main while loop.

but that's not hapanning, when user hit 1 program should print('second loop canceled') but program just starting executing main loop from the begining.

maybe question is not structured for to be the easily readeble and understable, but this is my first evere question there. Thanks in advance.

import keyboard
from time import sleep
first_loop,second_loop = True, True 
first_if,second_if, third_if = True, True, True
while first_loop:
    if first_if:
        print('to move to the second loop, hit (1), break main loop(0)')
        first_if = False 
    if keyboard.is_pressed('1'):
        if second_if:
            print('moving to second loop..')
            sleep(1)
            while True:
                if third_if:
                    print('moved second loop\nfor break second loop hit(1). for break main loop hit (0)')
                    third_if = False 
                if keyboard.is_pressed('0'):
                    print('main loop canceled.')
                    first_loop = False 
                    break
                elif keyboard.is_pressed('1'):
                    print('second loop canceled')
                    first_if = True 
                    break
    elif keyboard.is_pressed('0'):
        print('main loop canceled.')
        break
                


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source