Skip to content

Instantly share code, notes, and snippets.

@reganto
Created April 6, 2020 06:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reganto/4d41327bd45710f2b73b40cc71a3a190 to your computer and use it in GitHub Desktop.
Save reganto/4d41327bd45710f2b73b40cc71a3a190 to your computer and use it in GitHub Desktop.
A simple captcha with python
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