- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmult_practice.py
More file actions
Latest commit
151 lines (120 loc) · 4.18 KB
/
Copy pathmult_practice.py
File metadata and controls
151 lines (120 loc) · 4.18 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
"""
My son needed more practice with multiplication
and I made this little console game for him.
Launch with
python mult_practice.py
in your terminal/command line and let your kids play too!
"""
importsys
importrandom
# numbers from 0 to 12
NUMBERS=tuple(range(13))
# probabilities for each number, adjust to your needs
WEIGHTS= (0.01, 0.01, 0.01, 0.05, 0.05, 0.03, 0.11,
0.11, 0.11, 0.11, 0.05, 0.17, 0.18)
# response options for right and wrong cases
RESPONSE_CORRECT= ("Correct!", "That's right!", "Perfect!",
"Good job!", "You're a genius!", "Great!")
RESPONSE_WRONG= ("Try again!", "No, it isn't...",
"Give it another try!", "Think twice...")
# these are for the hints in the game
FACTORS= {4: (2, 2), 6: (2, 3), 8: (2, 4), 9: (3, 3)}
DISTRIBUTIONS= {11: (10, 1), 12: (10, 2)}
defget_help(n1, n2):
"""
For suitable numbers creates a representation by either its factors
or a sum of two numbers and returns an equation string with them.
"""
defget_factors(n):
ifninFACTORS:
return [str(i) foriinFACTORS[n]]
else:
returnFalse
defdistibute(n):
ifninDISTRIBUTIONS:
return [str(i) foriinDISTRIBUTIONS[n]]
else:
returnFalse
lst= []
fornin [n1, n2]:
s=" x ".join(get_factors(n)) ifget_factors(n) \
else"("+" + ".join(distibute(n)) +")"ifdistibute(n) \
elsestr(n)
lst.append(s)
return" x ".join(lst)
defround_setup(numbers, weights):
"""
For each round of the game randomly selects 2 numbers from the list
from 0 to 12 and returns the product of these two numbers to check
the player's input, the multiplication equation string and the equation
string with additional hints based on distibutive and associative
properties /see get_help() function/.
"""
n1=random.choice(random.choices(numbers, weights))
n2=random.choice(random.choices(numbers, weights))
equation="{} x {} = ".format(n1, n2)
equation_help=get_help(n1, n2)
product=n1*n2
returnproduct, equation, equation_help
defattempt_to_answer(product, equation, equation_help, attempts=0):
"""
Checks if the player's answer is correct and returns the number of attempt
the player used before asnwering correctly. After 2 incorrect attempts
it prints the equation with additional helpful hints.
"""
try:
answer=int(input())
except:
print("Enter a number")
answer=-1
attempts+=1
ifanswer==product:
print(random.choice(RESPONSE_CORRECT))
returnattempts
elifanswer==-1:
attempts-=1
returnattempt_to_answer(product, equation, equation_help, attempts)
else:
print(random.choice(RESPONSE_WRONG))
ifattempts>1:
print(equation+equation_help)
returnattempt_to_answer(product, equation, equation_help, attempts)
defplay_round():
"""
Launches the setup of each round, prints the equation for the round
and returns the numbers of attempts the player took to pass the round.
"""
product, equation, equation_help=round_setup(NUMBERS, WEIGHTS)
print(equation)
returnattempt_to_answer(product, equation, equation_help)
defplay_game():
"""
Launches the math practice game.
Prompts the player to continue or stop every 10 rounds
and keeps track of the progress.
"""
rounds= []
whileTrue:
rounds.append(play_round())
iflen(rounds) %10==0:
check=input("Do you want to continue? (y/n)")
ifcheck=='n':
break
total=len(rounds)
first=sum(1forrinroundsifr==1)
print(
"You got {} out of {} correctly at the first attempt!\nThat is {}%!" \
.format(first, total, round(100*first/total)))
defmain():
"""
Here starts the game!
"""
print("\nHi! Let's start the multiplication practice game!")
print("-------------------------------------------------\n")
try:
play_game()
except:
print("Unexpected input... exiting!\n")
sys.exit(0)
if__name__=="__main__":
main()