我导入regex
以获取字符串中的字数,并且我尝试使用^
运算符。虽然,编译器将^
识别为正则表达式的一部分,而不是独占或运算符。这是我的代码:
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)
我收到错误的行是elif
。基本上,有没有办法告诉编译器我想使用独占或运算符?提前谢谢!
答案 0 :(得分:1)
如果你可以添加错误跟踪,那就太好了。我怀疑你需要在每个布尔语句上加上括号,如下所示:
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):
答案 1 :(得分:0)
运算符^
的优先级高于运算符in
。你的表达式相当于"will" in (question ^ "can") in (question ^ "did") ...
,而你可能想要它("will" in question) ^ ("can" in question) ^ ("did" ...
。因此,请使用括号来消除表达式的歧义。