Created
March 1, 2022 18:32
-
-
Save SushiWaUmai/859231609324bc3123118df0e569e2b1 to your computer and use it in GitHub Desktop.
Calculation Trainer
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
import random | |
import time | |
from art import text2art | |
import argparse | |
import math | |
def create_problem(min_num, max_num): | |
# choose a random operator | |
op = random.choice(['+', '-']) | |
# create a random addition or substraction problem | |
if op == '+': | |
n1 = random.randint(min_num, max_num) | |
n2 = random.randint(min_num, max_num) | |
elif op == '-': | |
n1 = random.randint(min_num, max_num) | |
n2 = random.randint(min_num, n1) | |
# create the problem string | |
problem = f"{n1} {op} {n2}" | |
answer = eval(f"{n1} {op} {n2}") | |
# convert the problem string to an art | |
problem_art = text2art(problem) | |
answer_art = text2art(str(answer)) | |
return problem_art, answer_art | |
def main(): | |
calc_times = [] | |
min_num, max_num, n_problems = get_args() | |
print(f"Generating {n_problems} problems between {min_num} and {max_num}") | |
global calculation_time | |
for _ in range(n_problems): | |
problem, answer = create_problem(min_num, max_num) | |
print("Problem:") | |
print(problem) | |
# measure the time it takes to answer the problem | |
start_time = time.time() | |
input() | |
calc_time = time.time() - start_time | |
calc_times.append(calc_time) | |
print("The answer is:") | |
print(answer) | |
print(f"You answered the problem in {calc_time:.2f} seconds") | |
a = input() | |
if a == 'q': | |
break | |
# print the average calculation time | |
print(f"The average calculation time is {sum(calc_times)/len(calc_times):.2f} seconds") | |
def get_args(): | |
parser = argparse.ArgumentParser(description="Generate random math problems") | |
parser.add_argument("--min-num", default=10, type=int, help="The possible minimum number in the problem") | |
parser.add_argument("--max-num", default=100, type=int, help="The possible maximum number in the problem") | |
parser.add_argument("--n-problems", default=10, type=int, help="The number of problems to generate") | |
args = parser.parse_args() | |
if args.min_num > args.max_num: | |
args.max_num = math.floor(args.min_num + math.pow(10, math.ceil(math.log10(args.min_num)))) | |
return args.min_num, args.max_num, args.n_problems | |
if __name__ == "__main__": | |
main() |
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
art==5.4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment