You're using a slightly odd combination of boolean arithmetic on integers, and integer arithmetic on booleans.
Consider:
if (user.count(":-)") or user.count(" :-) ")) > (user.count(":-(") or user.count(" :-( ")):
Suppose the input is ":-)"
– the if statement is equivalent to:
if (1 or 0) > (0 or 0):
or
is a boolean operator, so the numbers are cast to booleans (1
becomes True
, 0
becomes False
), so the condition is equivalent to:
if (True or False) > (False or False):
i.e. True > False
.
Now, >
is a numeric operator, so Python casts the booleans to numbers (True
becomes 1
, False
becomes 0
) and 1 > 0
so the predicate is true. Fine.
But what if the input is ":-) :-) :-("
? Substituting in the counts, we get:
if (2 or 1) > (1 or 0):
(note here that the middle smiley is surrounded by spaces so it is [mistakenly?] counted twice)
Now, (2 or 1)
is True
, but (1 or 0)
is also True
, so True > True
evaluates to false.
Similarly, when your final predicate is evaluated against an empty string ...
elif (user.count(":-)") or user.count(" :-) ")) == (user.count(":-(") or user.count(" :-( ")):
... this is just (0 or 0) == (0 or 0)
i.e. True
.
If you simplified the code to:
if user.count(":-)") > user.count(":-("):
and similarly for the sad
and unsure
cases, then you'll get the expected results.
I'm not entirely sure why want to count " :-) "
twice – that feels like an mistake, because everything that matches " :-) "
will also match ":-)"
– but if you wanted to combine the counts of two popular emoticons then you could just add their counts rather than using or
, e.g.:
if (user.count(":-)") + user.count(":)")) > (user.count(":-(") + user.count(":(")):