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