Created
April 6, 2020 06:18
-
-
Save reganto/4d41327bd45710f2b73b40cc71a3a190 to your computer and use it in GitHub Desktop.
A simple captcha with python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from random import choice | |
from operator import add, mul, sub | |
def captcha(): | |
try_count = 0 | |
while True: | |
numbers = [number for number in range(1, 5)] | |
operators_functions = [add, mul, sub] | |
operators_functions_litrals_map = {add: "+", mul: "*", sub: "-"} | |
first_num = choice(numbers) | |
second_num = choice(numbers) | |
operator = choice(operators_functions) | |
question = f"Solve Question: {first_num}{operators_functions_litrals_map.get(operator)}{second_num} = " | |
answer = int(input(question)) | |
expected_answer = operator(first_num, second_num) | |
if answer != expected_answer: | |
try_count += 1 | |
print(f"try again, incorrent attempts: {try_count}") | |
elif answer == expected_answer: | |
return "pass" | |
if try_count == 3: | |
return "failed" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment