0

I'm trying to create a console menu in python where the options in the menu are listed 1 or 2. The selection of the number will open the next menu.

I decided to try and use a while loop to display the menu until the right number is selected, but I'm having an issue with the logic.

I want to use NOR logic as in if one or both values are true it returns false and the loop should break when false, however the loop just keeps looping even when I enter either 1 or 2.

I know I could use while True and just use break which is how I normally do it, I was just trying to achieve it a different way using logic.

while not Selection == 1 or Selection == 2:
    Menus.Main_Menu()
    Selection = input("Enter a number: ")
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
Amzo
  • 3
  • 1
  • `not Selection == 1 or Selection == 2` is **not** the same as `not (Selection == 1 or Selection == 2)`. You probably meant the latter. – Mad Physicist May 20 '19 at 19:20

2 Answers2

0

not has higher precedence than or; your attempt is parsed as

while (not Selection == 1) or Selection == 2:

You either need explicit parentheses

while not (Selection == 1 or Selection == 2):

or two uses of not (and the corresponding switch to and):

while not Selection == 1 and not Selection == 2:
# while Selection != 1 and Selection != 2:

The most readable version though would probably involve switching to not in:

while Selection not in (1,2):
chepner
  • 497,756
  • 71
  • 530
  • 681
  • I didn't realise about the higher precedence's. Switching to: ```while not (Selection == "1" or Selection == "2")```: worked, and seems like the input numbers were saved as strings and not int so I had to use quotes – Amzo May 20 '19 at 20:20
0

The NOR you want is either

not (Selection == 1 or Selection == 2)

or alternatively

Selection != 1 and Selection != 2

The two expressions above are equivalent to each other, but not to

not Selection == 1 or Selection == 2

This is equivalent to

Selection != 1 or Selection == 2

and thus to

not (Selection == 1 and Selection != 2)
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264