-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmathstudy.py
More file actions
127 lines (111 loc) · 3.08 KB
/
mathstudy.py
File metadata and controls
127 lines (111 loc) · 3.08 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
import tkinter as tk
import tkinter.simpledialog as simpledialog
import random
import sys
from decimal import Decimal
from PIL import Image, ImageTk
root = tk.Tk()
canvas = tk.Canvas(root, width=600, height=500, background="#f5c9ef")
canvas.grid(columnspan=3, rowspan=6)
# logo:
logo = Image.open("logo.png")
logo = ImageTk.PhotoImage(logo)
logo_label = tk.Label(image=logo)
logo_label.image = logo
logo_label.grid(column=1, row=0)
# Instructions
instructions = tk.Label(
root,
text="Select Level to Get 10 Questions: ",
font="Raleway",
bg="#f5c9ef",
fg="black",
)
instructions.grid(
column=1,
row=1,
)
# Definitions:
def run(x):
# create a label widget
my_result_var = tk.StringVar()
my_ask_var = tk.StringVar()
my_score_var = tk.StringVar()
result_label = tk.Label(root, height=1, width=20, textvariable=my_result_var)
result_label.grid(column=1, row=4)
ask_label = tk.Label(root, height=1, width=20, textvariable=my_ask_var)
ask_label.grid(column=1, row=3)
score_label = tk.Label(root, height=1, width=20, textvariable=my_score_var)
score_label.grid(column=0, row=3)
score = 0
level = x
for i in range(10):
summand_a = generate_integer(level)
summand_b = generate_integer(level)
result = Decimal(summand_a) + Decimal(summand_b)
for j in range(3):
try:
my_ask_var.set(f"{summand_a} + {summand_b} = ?")
answer = simpledialog.askstring("What is the answer?", "Answer: ", parent = root)
print(answer, result)
if answer == None:
sys.exit()
except (ValueError, TypeError):
pass
if Decimal(answer) == result:
evaluate = "Correct answer!"
my_result_var.set(evaluate)
score += 1
my_score_var.set(score)
break
elif j < 2:
evaluate = "EEE"
my_result_var.set(evaluate)
pass
else:
evaluate = f"{summand_a} + {summand_b} = {result}"
my_result_var.set(evaluate)
break
my_score_var.set(f'Your Score: {score}')
def generate_integer(level):
if level == 1:
return str(random.randint(0, 90) / 10)
elif level == 2:
return str(random.randint(0, 900) / 100)
elif level == 3:
return str(random.randint(0, 9000) / 1000)
# Level buttons:
level_button1 = tk.Button(
root,
text="Level 1",
command=lambda: run(1),
font="Raleway",
bg="white",
fg="black",
height=2,
width=15,
)
level_button1.grid(column=0, row=2)
level_button2 = tk.Button(
root,
text="Level 2",
command=lambda: run(2),
font="Raleway",
bg="white",
fg="black",
height=2,
width=15,
)
level_button2.grid(column=1, row=2)
level_button3 = tk.Button(
root,
text="Level 3",
command=lambda: run(3),
font="Raleway",
bg="white",
fg="black",
height=2,
width=15,
)
level_button3.grid(column=2, row=2)
root.mainloop()