我正在尝试用python创建一个控制台菜单,菜单中的选项列出了1或2。选择数字将打开下一个菜单。
我决定尝试使用while循环来显示菜单,直到选择了正确的数字,但我在逻辑上遇到了问题。
我想使用NOR逻辑,因为如果其中一个或两个值为真,则返回false,当为false时,循环应该中断,但是,即使我输入1或2,循环也会继续循环。
我知道我可以使用while True和break,这是我通常的方式,我只是试图用一种不同的方式使用逻辑来实现它。
while not Selection == 1 or Selection == 2:
Menus.Main_Menu()
Selection = input("Enter a number: ")发布于 2019-05-21 03:21:01
not比or具有更高的优先级;您的尝试被解析为
while (not Selection == 1) or Selection == 2:您需要显式的圆括号
while not (Selection == 1 or Selection == 2):或者not的两种用法(以及相应的切换到and):
while not Selection == 1 and not Selection == 2:
# while Selection != 1 and Selection != 2:不过,最具可读性的版本可能需要切换到not in
while Selection not in (1,2):发布于 2019-05-21 03:23:34
你想要的也不是
not (Selection == 1 or Selection == 2)或者另选地
Selection != 1 and Selection != 2上面的两个表达式彼此等价,但不等于
not Selection == 1 or Selection == 2这相当于
Selection != 1 or Selection == 2因此,为了
not (Selection == 1 and Selection != 2)https://stackoverflow.com/questions/56226905
复制相似问题