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!

2
  • Could you explain the logic behind your code? Commented Mar 12, 2017 at 6:37
  • I do not see any conflict in your example. Regex has nothing to do with your error. Commented Mar 12, 2017 at 6:48

2 Answers 2

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): 
Sign up to request clarification or add additional context in comments.

2 Comments

It didn't work. The error is unclosed string on line 10. Did you forget a quote?
That would be because of a typo on your part.
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.