-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
123 lines (91 loc) · 4.97 KB
/
Copy pathmain.py
File metadata and controls
123 lines (91 loc) · 4.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# NOTES
# my code is separating into two blocks where one block defines all the operation functions and the other block chooses which function to execute based on the user --option. i should move
# one (or both) blocks of code into separate .py files to avoid clutter.
#
# also, i think it'd be cleaner to define plicand and plier as global variables, and just declare them in the function before using them. so the code isn't repeated.
import typer
import random
import time
import math
from typing import Annotated
from stopwatch import Stopwatch, profile
app = typer.Typer()
timer = Stopwatch()
num_correct = 0
@app.command()
def test(
num_of_problems: Annotated[int, typer.Argument()], lower_bound: Annotated[int, typer.Argument()] = 3, upper_bound: Annotated[int, typer.Argument()] = 100, multiples_of: Annotated[int, typer.Argument()] = 1, time: Annotated[bool, typer.Option(help="enables a timer")] = True, primes_only: Annotated[bool, typer.Option(help="only prime numbers. currently only supports 2-100")] = False,zen: Annotated[bool, typer.Option(help="problems continue until you say stop")] = False, sums: Annotated[bool, typer.Option(help="addition problems")] = False, avg_time: Annotated[bool, typer.Option(help="display average time per problem solved")] = False,
):
# ARGUMENTS: num_of_problems, lower_bound, upper_bound, multiples_of
# OPTIONS: time, primes_only, zen, sums,
primes_list = (2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97,)
# primes_list is used for --primes_only
# can't i use range() for this?
def random_digit_mult():
if primes_only == False:
for n in range(num_of_problems):
plicand = random.randrange(lower_bound,upper_bound,multiples_of)
plier = random.randrange(lower_bound,upper_bound,multiples_of)
prod = plicand * plier
# generate new multiplicand multiplier and product.
# multiples_of is used here in case the user wants to work on multiples of 5 for example
print("\n"+str(plicand)+" * "+str(plier))
print("\nproduct:")
user_ans = int(input())
# prints plicand and plier, awaits user input.
if user_ans == prod:
print("\n"+str(prod)+" is correct")
num_correct = num_correct + 1
else:
print("\n"+str(user_ans)+" is incorrect. "+str(prod)+" was the correct answer")
# prints "x was correct/incorrect" after the user responds
def primes_only_mult():
for n in range(num_of_problems):
plicand = random.choice(primes_list)
plier = random.choice(primes_list)
prod = plicand * plier
# if --primes_only is selected, the program chooses a random number from the primes_list to use as plicand and plier
print("\n"+str(plicand)+" * "+str(plier))
print("\nproduct:")
user_ans = int(input())
if user_ans == prod:
print("\n"+str(prod)+" is correct")
num_correct = num_correct + 1
else:
print("\n"+str(user_ans)+" is incorrect. "+str(prod)+" was the correct answer")
def random_digit_sum(addend1: int, addend2: int):
global num_correct
numbergroup = (addend1,addend2)
summation = sum(numbergroup)
# can we condense this into one line?
print("\n"+str(addend1)+" + "+str(addend2))
print("\nsum:\n")
user_ans = int(input())
if user_ans == summation:
print("\n"+str(summation)+" is correct")
num_correct += 1
else:
print("\n"+str(user_ans)+" is incorrect. "+str(summation)+" was the correct answer")
# i wrote this function in a different way just to see if it'll work the same, and if it has advantages over the other way.
# it's one line shorter, if you don't count comment or spaces. that's something.
timer.start()
if primes_only == True:
primes_only_mult()
elif sums == True:
for i in range(num_of_problems):
random_digit_sum(random.randrange(lower_bound,upper_bound),random.randrange(lower_bound,upper_bound))
#need to reset the value of addend1 and addend2, otherwise prints the same # repeatedly
else:
random_digit_mult()
timer.stop()
# timer starts either way, but only prints elapsed time if --time option is used
test_done_msg = "\n" + str(num_correct) + "/" + str(num_of_problems) + " correct. that's " + str(round(num_correct / num_of_problems * 100, 2)) + "%!"
test_done_time = "time elapsed: " + str(round(timer.elapsed, 2)) + "s"
test_avg_time = "average time per problem: " + str(round(timer.elapsed / num_of_problems, 2)) + "s"
print(test_done_msg)
if timer:
print(test_done_time)
if avg_time:
print(test_avg_time)
if __name__ == "__main__":
app()