-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComplement and Difference Trainer.py
More file actions
117 lines (92 loc) · 3.83 KB
/
Complement and Difference Trainer.py
File metadata and controls
117 lines (92 loc) · 3.83 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
import tkinter as tk
from tkinter import messagebox
import random
class SetTrainerApp:
def __init__(self, root):
self.root = root
self.root.title("Complement and Difference Trainer")
self.root.geometry("600x500")
self.score = 0
self.total_questions = 0
self.create_widgets()
self.generate_sets()
def create_widgets(self):
self.title_label = tk.Label(self.root, text="Complement & Difference Trainer",
font=("Arial", 16, "bold"))
self.title_label.pack(pady=10)
self.set_display = tk.Label(self.root, text="", font=("Arial", 12))
self.set_display.pack(pady=10)
self.question_label = tk.Label(self.root, text="", font=("Arial", 12, "bold"))
self.question_label.pack(pady=5)
self.answer_entry = tk.Entry(self.root, width=40)
self.answer_entry.pack(pady=5)
self.submit_btn = tk.Button(self.root, text="Submit Answer",
command=self.check_answer)
self.submit_btn.pack(pady=5)
self.next_btn = tk.Button(self.root, text="Next Question",
command=self.next_question)
self.next_btn.pack(pady=5)
self.score_label = tk.Label(self.root, text="Score: 0/0",
font=("Arial", 12))
self.score_label.pack(pady=10)
def generate_sets(self):
universal = set(random.sample(range(1, 21), 10))
self.U = universal
self.A = set(random.sample(list(universal), random.randint(3, 6)))
self.B = set(random.sample(list(universal), random.randint(3, 6)))
self.set_display.config(
text=f"Universal Set U: {sorted(self.U)}\n"
f"Set A: {sorted(self.A)}\n"
f"Set B: {sorted(self.B)}"
)
self.questions = [
("A - B", self.A - self.B),
("B - A", self.B - self.A),
("Complement of A", self.U - self.A),
("Complement of B", self.U - self.B),
]
self.current_index = 0
self.load_question()
def load_question(self):
question_text = f"Find: {self.questions[self.current_index][0]}"
self.question_label.config(text=question_text)
self.answer_entry.delete(0, tk.END)
def parse_input(self, user_input):
try:
if user_input.strip() == "":
return set()
return set(int(x.strip()) for x in user_input.split(","))
except:
return None
def check_answer(self):
user_input = self.answer_entry.get()
user_set = self.parse_input(user_input)
if user_set is None:
messagebox.showerror("Invalid Input",
"Enter numbers separated by commas (e.g., 1,2,3)")
return
correct_set = self.questions[self.current_index][1]
self.total_questions += 1
if user_set == correct_set:
self.score += 1
messagebox.showinfo("Correct!", "Well done!")
else:
messagebox.showinfo(
"Incorrect",
f"Correct Answer: {sorted(correct_set)}"
)
self.score_label.config(
text=f"Score: {self.score}/{self.total_questions}"
)
def next_question(self):
if self.current_index < len(self.questions) - 1:
self.current_index += 1
self.load_question()
else:
messagebox.showinfo("Round Complete",
f"Final Score: {self.score}/{self.total_questions}")
self.generate_sets()
if __name__ == "__main__":
root = tk.Tk()
app = SetTrainerApp(root)
root.mainloop()