我不明白为什么“或”运算符在这种情况下没有按预期工作。
这是代码:
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!')
有人可以解释为什么会这样吗?