简单if/else语句(不等于或条件)的问题-Python

简单if/else语句(不等于或条件)的问题-Python,python,python-3.x,Python,Python 3.x,我对Python比较陌生,我想知道我是否可以换一双新眼睛。我一直在编写一个简单的if/elif结构,其中not!=,或内部的条件句。我的目标是让它遍历列表中的每个项,确定该项是否等于三个指定字符串值中的一个,如果不等于,则生成一个错误。我不能完全确定为什么这段代码不能正确执行,但我非常感谢您愿意提供的任何帮助 if yes_or_no_further_data.lower() == 'yes' or yes_or_no_further_data.lower() =='y': #T

我对Python比较陌生,我想知道我是否可以换一双新眼睛。我一直在编写一个简单的if/elif结构,其中not!=,或内部的条件句。我的目标是让它遍历列表中的每个项,确定该项是否等于三个指定字符串值中的一个,如果不等于,则生成一个错误。我不能完全确定为什么这段代码不能正确执行,但我非常感谢您愿意提供的任何帮助

if yes_or_no_further_data.lower() == 'yes' or yes_or_no_further_data.lower() =='y':
        #The following code asks the user what further information they would like to specify, and possible calculations that could be derived from this
        print('\nPlease select the letter(s) of the following information you would like to enter, spaced out: \n')
        print('\tA) Precise byproducts from the Nuclear Reaction (gives more precise energy output) \n')
        print('\tB) The total amount of energy lost \n')
        print('\tC) What your specified amount of energy could potentially power \n')
    
        userwish = input()
        ightybro = userwish.strip(',').split()
    
    
        for i in ightybro:
            if i.lower() != 'a' or i.lower() != 'b' or i.lower() != 'c':
                print(i)
                print('An error occurred, you entered an incorrect letter! Please enter the letter(s) A, B, or C (you can also enter multiple)')
                userwish = input()
                ightybro = userwish.strip().split()
                if i.lower() == 'a' or i.lower() == 'b' or i.lower() == 'c':
                    break
            else:
                continue
        
我不完全清楚为什么会这样!=“if”结构未正确执行。当我在控制台中输入“abc”时,程序仍然会通过if结构,尽管每个值都等于三个指定值中的一个

提前谢谢

只需更改这一行:

if i.lower() != 'a' or i.lower() != 'b' or i.lower() != 'c':
为此:

if i.lower() != 'a' and i.lower() != 'b' and i.lower() != 'c':
因为如果输入与所有3个字母不同,则要运行错误消息。当前,if语句始终为True(默认情况下,输入将与3个字母中的某些字母不同),并且当您说:

if i.lower() != 'a' or i.lower() != 'b' or i.lower() != 'c'
此表达式返回
True
if
i.lower()!=a
或if
i.lower()!='b'
等等

因此,如果
i==“b”
,第一部分为true,那么整个表达式为
true
。这是一个合乎逻辑的“或”

如果i.lower不是在['a','b','c']中,你的意思可能是:
。仅当
i.lower()
与所有选项不同时,才会返回
True
。或者,在布尔逻辑中:

如果i.lower()!='a'和i.lower()!='b'和i.下()!='c':


只有当
i.lower()
与所有
'a',b',c'
不同时,才会返回
True
将or条件更改为and

if i.lower() != 'a' and i.lower() != 'b' and i.lower() != 'c':
或者你也可以这样做

if i.lower() not in ['a', 'b', 'c']:
对于简单的解决方案,您可以使用
any()

if any(i.lower() != 'a' and i.lower() != 'b' and i.lower() != 'c' for i in ightybro):
    #do something

啊,谢谢你!我没有评估我打字的逻辑。谢谢你!不客气。别忘了接受一些答案,这样你的问题就会从无人回答的队伍中消失,干杯这是不对的。您必须使用any,因为如果any i与3不同,您希望运行错误letters@IoaTzimas如果i.lower()='a'或i.lower()='b'或i.lower()='c':break
看起来有误,你是对的。还有,输出是什么?为了确保我理解:您想知道为什么,例如,
'a'!='“a”或“a”!='b'或a'!='c'
是真的吗?那么,对于
False、True或True,您希望得到什么结果?请参见