Always true when testing if string == various OR'ed alternatives

前端 未结 5 1665
时光取名叫无心
时光取名叫无心 2020-11-30 14:57

So the problem I am currently having is that my program always calls the \'md5cypher\' class which I have defined, even if the input is not in that list:

def         


        
5条回答
  •  心在旅途
    2020-11-30 15:19

    You have a fundamental misunderstanding of how boolean operators work in python.

    You have to consider the operator precedences, and then your condition becomes

    if (toe=='md5') or ('M') or ('m') or ('Md5') or ('MD5'):
    

    which is equivalent to

    if (toe=='md5') or True:
    

    which of course is always true. A solution to your problem would be

    if toe.lower() in ('m', 'md5'):
    

提交回复
热议问题