0

Entire code:

message = input("Enter a message: ")
etimes = 0
print("Your message is",len(message),"characters long")

print("The most common letter in the english language, 'e', is ", end="")
for letter in message:
    if "e" in letter:
        etimes += 1

if "e" or "E" in message:
    print("in your message", etimes, "times")
else:
    print("is not in your message")

Why does

if "e" in letter:
            etimes += 1

work properly, but if i were to change it to

if "e" or "E" in letter:
            etimes += 1

it does not? if i use this code it will tell me there are as many "e"s as my message is long ex: message = "hello" would return "Your message is 5 characters long The most common letter in the english language, 'e', is in your message 1 times" Why is this?

justin
  • 11
  • 1

2 Answers2

1

In many cases, the way you state a logical condition in a programming language doesn't match normal conversational English. The or operator in your code is separating two expressions, each of which is evaluated to see if it evaluates to Boolean True or False.

Think of it this way: you are actually saying "if expr1 is true, and/or expr2 is true, do something". Expr1 is the literal "e", which always evaluates to True -- roughly speaking values other than an empty string or zero or None evaluate to True. Expr2 is the expression "E" in letter, which can be True or False depending on the value of letter. However since expr1 is True, expr2 will never be evaluated -- an or won't be evaluated past the first True value.

The way to state your logical test in English is "if "e" is in letter or "E" is in letter". In code, you could use the exact same syntax.

You could also make the same test more straightforward, e.g. "if letter.lower() == "e" or if letter in ("e", "E").

Chris Johnson
  • 20,650
  • 6
  • 81
  • 80
0
if "e" or "E" in letter

That line could be broken down as follows:

if "e": or if "E" in letter:

To do it right, you would need to say:

if letter == "e"  or letter == "E":

OR you could do:

if letter in ['e', 'E']:
Totem
  • 7,189
  • 5
  • 39
  • 66
  • I would argue to do it right you need `if 'e' in letter.lower():` – SethMMorton Feb 08 '14 at 20:29
  • or, better still, `letter.lower()=='e'` since each letter is single character result of intreating over `message. – dawg Feb 08 '14 at 20:30
  • that makes a lot more sense! but what exactly would `if "e"` be testing? @Totem – justin Feb 08 '14 at 20:32
  • `if 'e'` is always `True` just as `if 1` or `if True` – dawg Feb 08 '14 at 20:33
  • basically it would return True or False. for instance if 'e': will return True. – Totem Feb 08 '14 at 20:33
  • an empty string or empty list for instance would return False – Totem Feb 08 '14 at 20:33
  • Rather than `if letter in ['e', 'E']` you can do `if letter in 'eE'` which is both more obvious and faster. If you want to test against a larger list like thing, use a set: `if obj in {obj1, obj2, etc}` – dawg Feb 08 '14 at 21:06
  • @dawg Since `if 'e' in letter.lower():` is not the same as `letter.lower()=='e'` and we don't know the OP's intent, we should be cautious in recommending it. – SethMMorton Feb 08 '14 at 21:14
  • @dawg Also, `if letter in 'eE'` will match 'eE', so it's not the same as `if letter in ['e', 'E']`. – SethMMorton Feb 08 '14 at 21:15
  • @SethMMorton: You are correct about `'eE'` matching `if letter in 'eE'` but in the contact presented, iterating of a string letter by letter, having 'eE' in a single character is unlikely and would indicate other issues. – dawg Feb 08 '14 at 21:27