When I enter 4, the program does not ask to press Enter and does not close, but immediately writes what is in the else block, that is, the Unknown command. Type again Help solve the issue, well, you can still shorten the code

answer1 = input ('y/n: ') while True: if answer1 == 'y': while True: answer2 = str (input ('\nIf you want to exit type 4\n1/2/3/(4 close): ')) if answer2 == '1': print ('1') elif answer2 == '2': print ('2') elif answer2 == '3': print ('3') elif answer2 != '1' or answer2 != '2' or answer2 != '3' or answer2 != '4' or answer2 == '': print ('Unknown command. Type again') elif answer2 == '4': input ('Click Enter to exit') exit () continue elif answer1 == 'n': input ('Click Enter to exit') exit () else: print ('Unknown command. Type again') continue 

3 answers 3

Expression

 answer2 != '1' or answer2 != '2' or answer2 != '3' or answer2 != '4' or answer2 == '' 

light movement of the law of de morgan turns into

 not (answer2 == '1' and answer2 == '2' and answer2 == '3' and answer2 == '4') or answer2 == '' 

The expression in brackets is always false (all four conditions cannot be fulfilled simultaneously). Turns out

 (not False or X) => (True or X) => True 

Those. the original expression is always true, so the print ('Unknown command. Type again') branch will always be executed (if the previous branches are not executed). The next branch, if execution, simply does not reach.

Correct condition:

 answer2 != '1' and answer2 != '2' and answer2 != '3' and answer2 != '4' or answer2 == '' 

In short, in Python you can write as:

 answer2 not in {'1', '2', '3', '4'} or not answer2 

But it is best to move this thread to the end and turn it simply into an else .

    You can try this, replace the verification operation.

     elif answer2 != '1' or answer2 != '2' or answer2 != '3' or answer2 != '4' or answer2 == '' 

    just else

     if input('y/n: ') == 'y': while True: answer2 = input('\nIf you want to exit type 4 \n 1/2/3/(4 close):\n') if answer2 == '1': print('1') elif answer2 == '2': print('2') elif answer2 == '3': print('3') elif answer2 == '4': input('Click Enter to exit') exit() else: print('Unknown command. Type again') else: input('Click Enter to exit') exit() 

    You can also try to immediately set the int values ​​when entering the numbers and compare the resulting value. Attempts to enter a word would be processed except for the exception except ValueError (this option is suitable if the user is only offered to select the digital version of the value)

     if input('y/n: ') == 'y': while True: try: answer2 = int(input('\nIf you want to exit type 4 \n 1/2/3/(4 close):\n')) if answer2 == 1: print('1') elif answer2 == 2: print('2') elif answer2 == 3: print('3') elif answer2 == 4: input('Click Enter to exit') exit() elif answer2 > 4: print('Unknown command. Type again') except ValueError: print('Unknown command. Type again') else: input('Click Enter to exit') exit() 
    • Thanks, I understood everything now - Alexander Bokanov

    To request input until y/n is received, exiting to get 'n' . Then, requesting the digits on 'y' and print them, going on '4' :

      [^yn] ╔════════╗ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€ β•‘ β•‘ β”‚ β•‘ START β•‘ └───────▢ β•‘ β•‘ ─┐ β•šβ•β•β•β•β•β•β•β•β• β”‚ β”‚ β”‚ β”‚ y β”‚ β–Ό β”‚ [123] ╔════════╗ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€ β•‘ β•‘ β”‚ β”‚ β•‘ DIGITS β•‘ β”‚ n └───────▢ β•‘ β•‘ β”‚ β•šβ•β•β•β•β•β•β•β•β• β”‚ β”‚ β”‚ β”‚ 4 β”‚ β–Ό β”‚ ╔════════╗ β”‚ β•‘ END β•‘ β—€β”˜ β•šβ•β•β•β•β•β•β•β•β• 

    You can define an expect() function that accepts a dictionary of expected responses and corresponding actions:

     #!/usr/bin/env python3 import sys def exit(): input('Press <Enter> to exit') sys.exit() def expect_digits(): options = {'4': exit} for d in '123': options[d] = lambda d=d: print(d) expect(options) def expect(options): while True: answer = input('Input ' + '|'.join(options)).strip() if answer in options: options[answer]() else: print('Unknown command. Try again') expect({'y': expect_digits, 'n': exit}) 

    An example .

    For two levels of questions, it is not necessary to define functions, and you can write cycles directly with simple conditions ( if answer in ["1", "2", "3"] ). Using the expect() function makes it easy to expand the code for more nested questions.