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

安藤:

我从文本文件“ 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)

和鲍伯你的叔叔。谢谢男孩们!:)

D西尔维罗:

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

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

print(validation)

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章