0

I'm importing regex for a word count in a string, and I'm trying to use the ^ operator. Although, the compiler is recognizing the ^ as part of regex, and not the exclusive or operator. This is my code:

import re as regex

count = len(regex.findall(r'\w+', question))


if count < 3:

    magicball.say("No Spam Allowed!!")

elif "will" in question ^ "can" in question ^ "did" in question ^ "is" in question ^ "do" in question ^ "are" in question ^ "has" in question ^ "does" in question ^ "is" in question:

    verified = True
    print(oringinalquestion)

The line that I'm getting the error is where it says elif. Basically, is there a way to tell the compiler that I want to use the exclusive or operator? Thanks in advance!

DYZ
  • 55,249
  • 10
  • 64
  • 93
Nathan Chan
  • 303
  • 4
  • 13

2 Answers2

1

If you can add the error trace it would be great. My suspicion is that that you need to put brackets on every boolean statement like this:

elif ("will" in question) ^ ("can" in question) ^ ("did" in question) ^ ("is" in question) ^ ("do" in question) ^ ("are" in question) ^ ("has" in question) ^ ("does" in question) ^ ("is" in question):
Den1al
  • 617
  • 1
  • 10
  • 16
0

Operator ^ has higher precedence than operator in. Your expression is equivalent to "will" in (question ^ "can") in (question ^ "did") ..., while what you presumably want it ("will" in question) ^ ("can" in question) ^ ("did" .... So, use parentheses to disambiguate your expression.

DYZ
  • 55,249
  • 10
  • 64
  • 93