为什么我的 IF 语句和 OR 运算符没有给我预期的输出?

Why my IF statement and OR operator doesn't give me the expected output?

实际上我不确定这里出了什么问题,但代码并没有给我想要的东西。

从技术上讲,我想让它做的是,如果我写“0”,它会出现在字典中(可能更多,具体取决于它有多少项目)或 'N',它就会停止。但它不起作用。它总是 运行 if 而不是 else。

它是我看不到的明显的东西还是只是一个错误(不太可能)

from time import sleep

inventory = {}
character = {'Energy': 180}
inventory['Red Mushroom'] = {'Quantity': 1,
                                   'Description': 'It looks good for the Energy, but also a tasteful snack...',
                                   'Effect': 35}

def show_inve():
    sleep(1)
    mapear = {}
    if inventory == {}:
        print('Its empty...\n')
    else:
        for i, pos in enumerate(inventory):
            print(f'[{i}] {pos:<10}: {inventory[pos]["Quantity"]:>0}')
            mapear[str(i)] = pos

        while True:
            sleep(1)
            decision = input('Type the number of the item or N to leave: ').strip()
            if decision not in mapear or decision != 'N':
                sleep(1)
                print('Not an option.')
                continue
            else:
                break


show_inve()

您需要 and 操作员。 or 运算符检查任一条件是否为真。当我们放N的时候,当然不在mapear里面了,if decision not in mapear求值为True。因为它是一个逻辑 or 运算符并且 1 个条件的计算结果为真,所以它不会中断,而是会执行 if 语句中的块。

if decision not in mapear and decision != 'N':
    sleep(1)
    print('Not an option.')
                
else:
    break

这是andor的流程图