python - 使用用户输入验证生成的验证码

标签 python

我从文本文件“captcha.txt”中随机选择了一个验证码。 我已将这些召回的字符转换为一个变量,然后我可以使用该变量验证用户的输入。

import random 
import os
def captchasub():

    thisfolder = os.path.dirname(os.path.abspath(file))
    captchatxt = os.path.join(thisfolder, 'captcha.txt')

    with open(captchatxt) as text_file: 
        captcha = text_file.read()
        validation = print("".join(random.choices(captcha, k=6,)))
        x = input('Please Rewrite the Captcha Above:')
        if x != validation:
            print ('Stupid Robot')
            return captchasub()
        if x == validation:
            print ('Welcome')

captchasub()

但是,我在成功完成验证码时没有收到预期的“欢迎”打印信息。

我相信在 x != 验证之前一切都很好(甚至可能在 x = 输入时)。

编辑: 解决了!

validation = print("".join(random.choices(captcha, k=6,)))

简单更改为

validation = "".join(random.choices(captcha, k=6,))
print(validation)

还有鲍勃,你的叔叔。 谢谢 children ! :)

最佳答案

您的验证变量是None,您首先需要为其分配值,然后打印它,如下所示:)

validation = "".join(
    random.choices(
        captcha,
        k=6,
    ),
)

print(validation)

if x == validation:
    print('Welcome')
else:
    print('Stupid Robot')
    return captchasub()

关于python - 使用用户输入验证生成的验证码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63118800/

相关文章:

如果长度不等于 x,Python 将字符串替换为空

python - Django:为什么出现 KeyError: 'pk'

python - 我如何抓取该网站以便按列表对候选人进行分类?

使用 Amazon Cognito 进行身份验证的 Python 和 Flask

python - Django 聚合。如何将其值渲染到模板中?

Python 数组自动相互复制

python - wxpython 中静态文本的滚动条?

python - BeautifulSoup - 摆脱段落空白/换行符

Python:一个月中的周数

python idastar vs astar解决8个难题