简体   繁体   中英

How to create a simple input math captcha in Python?

I am struggling hard to create a simple input Math Captcha in Python.

This is what I tried

from random import randint

x = randint(1,10)
y = randint(1,10)

z = x + y

captcha_ans = input(f"What is the sum of {x} + {y} ? ")
captcha_ans = int(captcha_ans)

if captcha_ans == z:
    print("Great")
else:
    print("Try Again")

I wanted that if the answer is wrong, the code should not just ask to Try again but repeat the same question with/without changing the numbers.

I tried using a while statement for this, but couldn't understand how to do it.

It would be great, if someone could throw some light on the issue by improving the code.

Thanks in Advance

Try factorizing your code and inside a while loop check the answer. Check the comments for explanation

from random import randint

# create a function that will take input from use
# and check if it is correct or not
# if correct it will return True
# otherwise it will return False
def take_answer_from_user():
    x = randint(1,10)
    y = randint(1,10)

    z = x + y

    captcha_ans = input(f"What is the sum of {x} + {y} ? ")
    captcha_ans = int(captcha_ans)
    return captcha_ans == z

# For the first time, take input from user and store the True, False
# result in the variab;e is_correct
is_correct = take_answer_from_user()

# check if is_correct is False, that means user did not provide correct
# answer
# If wrong, use while loop unless answer is correct
while(not is_correct):
    print("Try Again")
    is_correct = take_answer_from_user()
# if user provides correct answer, then wile loop will be closed and
# finally print great
print('Great')
What is the sum of 2 + 9 ? 4
Try Again
What is the sum of 4 + 7 ? 5
Try Again
What is the sum of 10 + 6 ? 16
Great

Okay Your code is fine, it's just that you need to run it in loop

from random import randint

x = randint(1,10)
y = randint(1,10)

z = x + y
while True:
   captcha_ans = input(f"What is the sum of {x} + {y} ? ")
   captcha_ans = int(captcha_ans)

   if captcha_ans == z:
       print("Great")
       break
   else:
       print("Try Again")

It will print the same captcha until user is able to answer it correctly, you can try to even incorporate the number of attempts allowed, something like this:

from random import randint

x = randint(1,10)
y = randint(1,10)

z = x + y
attemptsLeft = 3
while True:
   captcha_ans = input(f"What is the sum of {x} + {y} ? ")
   captcha_ans = int(captcha_ans)

   if captcha_ans == z:
       print("Great")
       break
   else:
       print(f"Try Again, attempts left:{attemptsLeft}")
       attemptsLeft -= 1
   if attemptsLeft <= 0:
       print('You entered wrong answer three times, exiting...')
       break

This works fine. Run a while loop if original sum and user calculated sum are not equal, else break out the loop.

from random import randint
    
def captcha():
    x = randint(1,10)
    y = randint(1,10)
        
    z = x + y
        
    while True:
        captcha_ans = input(f"What is the sum of {x} + {y} ? ")
        captcha_ans = int(captcha_ans)
            
        if captcha_ans == z:
            print("Great")
            break
        else:
            print("Try Again")
                
captcha()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM