Home > Back-end >  Why can't I get the correct captcha even though the output is the same as the user input?
Why can't I get the correct captcha even though the output is the same as the user input?

Time:12-13

I'm trying to create a captcha prompt for a project that I've been working on. So far the code works except it results in 'Incorrect try again'. I've tried printing the generated number to test if I have been inputting the wrong answer, but it's the same. Is there something wrong with my code?

from captcha.image import ImageCaptcha
import random
import os

a = random.randint(1111, 9999)

image = ImageCaptcha(width=250,height=90)

text = a
generated_imge = image.generate(str(text))

image.write(str(a), 'captcha.png')
while True:
    abs = input('What is the captcha:')
    if abs == a:
        print('Correct')
        break
    else:
        print('Incorrect Try again')

CodePudding user response:

Input in Python is always a string. You can simply wrap it in int to make it consistent across your app.

    ...:     abs = int(input('What is the captcha:')) # Change is here, wrapped input in int
    ...:     if abs == a:
    ...:         print('Correct')
    ...:         break
    ...:     else:
    ...:         print('Incorrect Try again')
    ...: 
What is the captcha:3517
Correct

CodePudding user response:

you are comparing abs which is string with a which is int. you should change a to string with str(a)

  • Related