1

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 enc():
    global toe
    if toe=='md5' or 'M' or 'm' or 'Md5' or 'MD5':
        print("Md5 Encryption Cypher")
        md5cypher()
    else:
        print("Sha1 Encryption Cypher")
        shacypher()

What am I doing wrong?

Paul
  • 26,170
  • 12
  • 85
  • 119
user3556962
  • 353
  • 2
  • 4
  • 4

5 Answers5

4

In reality you're checking:

if (toe=='md5') or 'M' or 'm' or....

And since bool('M') is True, you will always succeed that check. Try this instead:

if toe.lower() in ('md5', 'm'):
viraptor
  • 33,322
  • 10
  • 107
  • 191
3

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

will always be evaluated to

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

What you want is:

if toe in ('md5', 'M', 'm', 'Md5', 'MD5'):
    print("Md5 Encryption Cypher")
    md5cypher()
.
.
.
Paul
  • 26,170
  • 12
  • 85
  • 119
s16h
  • 4,647
  • 1
  • 21
  • 33
1

Literally, what you want to do is:

if toe=='md5' or toe=='M' or toe=='m' or toe=='Md5' or toe=='MD5'

- each part of the composite condition should be a standalone condition.

But in Python you can do it in a more elegant way, as indicated in other answers:

if toe in ('md5', 'M', 'm', 'Md5', 'MD5')
BartoszKP
  • 34,786
  • 15
  • 102
  • 130
0

That is because checking a character will always return True therefore ensuring the if statement will execute.

MrAlias
  • 1,316
  • 15
  • 26
0

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'):
deets
  • 6,285
  • 29
  • 28
  • Will work incorrectly when `toe` is `'mD5'` (at least differently to what was specified in the OP) :-) – BartoszKP Apr 25 '20 at 20:34
  • You sure? Why won’t the lower affect it? – deets Apr 27 '20 at 06:12
  • It will, and that's the problem - there is no `'mD5'` in the list specified by the OP. – BartoszKP Apr 27 '20 at 13:20
  • I'm quite convinced that this is an oversight of the OP, but 6 years later I wonder how relevant this discussion is. – deets Apr 27 '20 at 14:57
  • As long as this thread is visible in search results, even if 20 years later, it should be helpful to other users. SO is not about answering a single user - it's about creating a knowledge base. So accuracy of an answer is actually very relevant. Perhaps it's an oversight - maybe you want to try to clear this out with the OP. As it stands, your proposed solution is not equivalent to what's requested in the question. – BartoszKP Apr 27 '20 at 16:25
  • Given that the OP explicitly stated that the highest voted answer that literally contains the same code as mine solves his or her problem, I maintain my answer as is. – deets Apr 28 '20 at 14:48
  • The highest voted answer is also invalid in this aspect :-) And, as I've mentioned - helping the OP directly is one result, but the more important outcome is helping all future readers :) – BartoszKP Apr 29 '20 at 16:11
  • We will have to agree to disagree here. If somebody wants to hang themselves, you are the guy providing them with a good, sturdy rope. I'm the guy trying to talk them out of it. Both are valid reactions to this situation. I'm 100% convinced that mD5 supposed not mean to hash with SHA. So - again - I maintain my solution. – deets Apr 29 '20 at 17:33
  • Sure, that makes sense. But noting this discrepancy explicitly in the answer doesn't hurt for sure as well. Thanks for your time, cheers! :-) – BartoszKP Apr 29 '20 at 18:17