python - 在 python 中使用 Regex 进行验证码验证
问题描述
您好我正在尝试使用正则表达式在 python 中验证一个简单的验证码
import re
captcha = "10 + 5"
numbers = re.findall('\d+', captcha)
num1 = int(numbers[0])
num2 = int(numbers[1])
sym = re.findall('\D+', captcha)
operator = sym[0]
# print(operator)
# captcha_answer = 0
if operator == "+":
captcha_answer = num1 + num2
if operator == "-":
captcha_answer = num1 - num2
if operator == "*":
captcha_answer = num1 * num2
print("captcha_answer =", captcha_answer)
我需要答案 15,但我收到一条错误消息,提示“名称 'captcha_answer' 未定义”,或者如果为 captcha_answer 定义一个错误值(如 0),则输出为 0 如何解决此问题。
提前致谢
解决方案
operator
是 +
(带有 blancs arround),因此它与您的 if 条件不匹配。删除空格使您的代码工作:
operator = sym[0].strip()
输出:
captcha_answer = 15
推荐阅读
- java - 从 MediaCodec 将相同的视频渲染到 2 个表面
- python - 在python中并行运行两个函数并将一个函数返回的参数作为参数传递给另一个函数
- visual-studio-code - 如何在 VsCode 终端中运行 MSyS2 Mingw64 bash 以及 cmd 提示符?
- powerbi - 如何在 PowerBI 中创建包含计算单元格而不是列的表格?
- c - 土耳其元音字母识别
- angular - 错误:
: 期待一个间谍,但在 Jamsine 中获得了功能 - firebase - 尝试处理 firebase 存储异常不起作用(颤振)
- javascript - Javascript 空验证在发送到数据库之前不起作用
- typescript - Typescript 会自动检测 void 函数吗?
- python - 一种用于销售预测和内核的热编码已死