-1
user = input()

if (user.count(":-)") or user.count(" :-) ")) > (user.count(":-(") or user.count(" :-( ")):
    print('happy')

elif (user.count(":-)") or user.count(" :-) ")) < (user.count(":-(") or user.count(" :-( ")):
    print('sad')

elif (user.count(":-)") or user.count(" :-) ")) == (user.count(":-(") or user.count(" :-( ")):
    print('unsure')

else:
    print('none')

It supposed to print 'none' when I write anything that does not include :-) or :-( but it prints 'unsure' everytime.

  • Does this answer your question? [Why does "a == x or y or z" always evaluate to True? How can I compare "a" to all of those?](https://stackoverflow.com/questions/20002503/why-does-a-x-or-y-or-z-always-evaluate-to-true-how-can-i-compare-a-to-al) – LeopardShark Apr 26 '23 at 10:52

2 Answers2

1

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(":(")):
motto
  • 2,888
  • 2
  • 2
  • 14
  • The counting of " :-) " twice was not the intention I thought it would solve the bug somehow, I simplified it to just the characters of smiley or sad faces, the problem was at the unsure & none blocks. Thank you, you gave me insights to learn from. – Ahmed A. Shaker Apr 26 '23 at 11:43
  • Glad to help, even if it was only against some interim version of your code :-) :-) :-( :-) – motto Apr 26 '23 at 11:45
0

There are two main problems with those if-statetments. First is that (user.count(":-)") or user.count(" :-) ")) > (user.count(":-(") or user.count(" :-( ")won't compare number of smiley faces to sad faces. It will first evaluate user.count(":-)") or user.count(" :-) ") which would be True if there is at least one smiley face or False if there is none. Then it would evaluate sad faces and then compare those results. Easiest workaround would be to sum those counts, something like this:

if (user.count(":-)") + user.count(" :-) ")) > (user.count(":-(") + user.count(" :-( ")):
    print('happy')
...

Second problem is that you last elif would result in True when there is no smiley or sad faces because it will simplify to:

...
elif (0 + 0) == (0 + 0):
    print('unsure')
...

easiest workaround would be to add another if-statement to check if there are none faces, something like this:

user = input()

if (user.count(":-)") + user.count(" :-) ") + user.count(":-(") + user.count(" :-( ") == 0):
    print('none')

elif (user.count(":-)") + user.count(" :-) ")) > (user.count(":-(") + user.count(" :-( ")):
    print('happy')

elif (user.count(":-)") + user.count(" :-) ")) < (user.count(":-(") + user.count(" :-( ")):
    print('sad')

elif (user.count(":-)") + user.count(" :-) ")) == (user.count(":-(") + user.count(" :-( ")):
    print('unsure')

jlipinski
  • 152
  • 12