Skip to content

Commit 9c1c79b

Browse files
authored
Add files via upload
1 parent 1aff7ee commit 9c1c79b

File tree

7 files changed

+133
-0
lines changed

7 files changed

+133
-0
lines changed

day34/data.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import requests
2+
3+
4+
parameters = {
5+
"amount": 10,
6+
"type": "boolean",
7+
"category": 18,
8+
}
9+
response = requests.get(url="https://opentdb.com/api.php", params=parameters)
10+
response.raise_for_status()
11+
data = response.json()
12+
question_data = data["results"]

day34/images/false.png

2.8 KB
Loading

day34/images/true.png

2.5 KB
Loading

day34/main.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from question_model import Question
2+
from data import question_data
3+
from quiz_brain import QuizBrain
4+
from ui import QuizInterface
5+
6+
question_bank = []
7+
for question in question_data:
8+
question_text = question["question"]
9+
question_answer = question["correct_answer"]
10+
new_question = Question(question_text, question_answer)
11+
question_bank.append(new_question)
12+
13+
quiz = QuizBrain(question_bank)
14+
quiz_ui = QuizInterface(quiz)
15+
16+
print("You've completed the quiz")
17+
print(f"Your final score was: {quiz.score}/{quiz.question_number}")

day34/question_model.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Question:
2+
3+
def __init__(self, q_text, q_answer):
4+
self.text = q_text
5+
self.answer = q_answer

day34/quiz_brain.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import html
2+
3+
4+
class QuizBrain:
5+
6+
def __init__(self, q_list):
7+
self.question_number = 0
8+
self.score = 0
9+
self.question_list = q_list
10+
self.current_question = None
11+
12+
def still_has_questions(self):
13+
return self.question_number < len(self.question_list)
14+
15+
def next_question(self):
16+
self.current_question = self.question_list[self.question_number]
17+
self.question_number += 1
18+
q_text = html.unescape(self.current_question.text)
19+
return f"Q.{self.question_number}: {q_text}"
20+
21+
def check_answer(self, user_answer):
22+
correct_answer = self.current_question.answer
23+
if user_answer.lower() == correct_answer.lower():
24+
self.score += 1
25+
return True
26+
else:
27+
return False
28+

day34/ui.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
from tkinter import *
2+
from quiz_brain import QuizBrain
3+
4+
5+
THEME_COLOR = "#375362"
6+
7+
8+
class QuizInterface:
9+
10+
def __init__(self, quiz_brain: QuizBrain):
11+
self.quiz = quiz_brain
12+
self.window = Tk()
13+
self.window.title("Quiz Game")
14+
self.window.config(padx=20, pady=20, bg=THEME_COLOR)
15+
16+
self.canvas = Canvas(height=250, width=300, bg="white")
17+
self.canvas.grid(column=0, row=1, columnspan=2, pady=50)
18+
self.question_text = self.canvas.create_text(
19+
150,
20+
125,
21+
width=280,
22+
text=f"test",
23+
fill=THEME_COLOR,
24+
font=("Arial", 20, "italic")
25+
)
26+
27+
self.score_text = Label(
28+
text="Score: 0",
29+
fg="white",
30+
bg=THEME_COLOR,
31+
font=("Arial", 20, "italic")
32+
)
33+
self.score_text.grid(column=1, row=0)
34+
35+
true_png = PhotoImage(file="images/true.png")
36+
self.true_button = Button(image=true_png, highlightthickness=0, command=self.answer_true)
37+
self.true_button.grid(column=0, row=2)
38+
39+
false_png = PhotoImage(file="images/false.png")
40+
self.false_button = Button(image=false_png, highlightthickness=0, command=self.answer_false)
41+
self.false_button.grid(column=1, row=2)
42+
43+
self.get_next_question()
44+
45+
self.window.mainloop()
46+
47+
def get_next_question(self):
48+
self.canvas.config(bg="white")
49+
if self.quiz.still_has_questions():
50+
self.score_text.config(text=f"Score: {self.quiz.score}")
51+
q_text = self.quiz.next_question()
52+
self.canvas.itemconfig(self.question_text, text=q_text)
53+
else:
54+
self.canvas.itemconfig(self.question_text, text="You've reached the end of the quiz.")
55+
self.true_button.config(state="disabled")
56+
self.false_button.config(state="disabled")
57+
58+
def answer_false(self):
59+
is_right = self.quiz.check_answer("False")
60+
self.give_feedback(is_right)
61+
62+
def answer_true(self):
63+
is_right = self.quiz.check_answer("True")
64+
self.give_feedback(is_right)
65+
66+
def give_feedback(self, is_right):
67+
if is_right:
68+
self.canvas.config(bg="green")
69+
else:
70+
self.canvas.config(bg="red")
71+
self.window.after(1000, self.get_next_question)

0 commit comments

Comments
 (0)