简体   繁体   English

如何在 Python 中创建简单的输入数学验证码?

[英]How to create a simple input math captcha in Python?

I am struggling hard to create a simple input Math Captcha in Python.我正在努力在 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.我尝试为此使用 while 语句,但不明白该怎么做。

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.尝试分解你的代码并在一个while循环中检查答案。 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.如果原始总和与用户计算的总和不相等,则运行 while 循环,否则中断循环。

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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