Python 中的逻辑运算符“OR”无法按预期工作?

问题描述

我不明白为什么“或”运算符在这种情况下没有按预期工作。

代码如下:

fuel = input()
liters = float(input())

if fuel != 'Gas' or fuel != 'Diesel' or fuel != 'Gasoline':
    print('Invalid fuel!')
else:
    if liters >= 25:
        if fuel == 'Gas' or fuel == 'Diesel' or fuel == 'Gasoline':
            print(f'You have enough {fuel.lower()}.')
    elif liters < 25:
        if fuel == 'Gas' or fuel == 'Diesel' or fuel == 'Gasoline':
            print(f'Fill your tank with {fuel.lower()}!')

输入:

Gas
25

输出: Invalid fuel

输出应该是You have enough gas.

当我将运算符更改为“and”时,代码工作正常。

if fuel != 'Gas' and fuel != 'Diesel' and fuel != 'Gasoline':
    print('Invalid fuel!')

有人能解释一下为什么会这样吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)