Skip to content

Commit c8d6513

Browse files
authored
Merge pull request #222 from sahil-s-246/sahil-s-246-patch-2
Quizzer
2 parents 9924551 + bdbce0f commit c8d6513

File tree

9 files changed

+165
-0
lines changed

9 files changed

+165
-0
lines changed

GAMES/Quizzer/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Quizzer
2+
3+
### A trivia game which uses the turtle module and the requests module to get questions from [Open Trivia Database ](https://opentdb.com/api_config.php) API
4+
5+
## About the Quiz Topics:
6+
- The topic of the quiz is **Anime/Manga** by default.
7+
- You can change it by changing the API parameters:
8+
1. Opening *Quizzer/data.py* in your editor.
9+
2. You can change *category* to "Computer_Science" or "GK"
10+
3. To get an all-in-one quiz remove *category*
11+
12+
## How to Run:
13+
- Open terminal and navigate to the file
14+
15+
```
16+
python main.py
17+
```
18+
19+
20+
## Sample Output
21+
![Sample Output](https://github.com/sahil-s-246/Python-project-Scripts/blob/sahil-s-246-patch-2/GAMES/Quizzer/Sample_Output.png)

GAMES/Quizzer/Sample_Output.png

54 KB
Loading

GAMES/Quizzer/data.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import requests
2+
3+
4+
# Index
5+
# GK : 9
6+
# Computer Science: 18
7+
# Anime / Manga : 31
8+
# to get all topics in one quiz , remove category from parameters
9+
NUM_OF_QUESTIONS = 20
10+
category = 31
11+
12+
parameters = {
13+
"category": category,
14+
"amount": NUM_OF_QUESTIONS,
15+
"type": "boolean"
16+
}
17+
18+
response = requests.get("https://opentdb.com/api.php", params=parameters)
19+
question_data = response.json()["results"]
20+
21+
22+
23+
24+
25+

GAMES/Quizzer/images/false.png

2.8 KB
Loading

GAMES/Quizzer/images/true.png

2.5 KB
Loading

GAMES/Quizzer/main.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
14+
quiz = QuizBrain(question_bank)
15+
ui = QuizInterface(quiz)
16+
17+
while quiz.still_has_questions():
18+
quiz.next_question()
19+
20+
print("You've completed the quiz")
21+
print(f"Your final score was: {quiz.score}/{quiz.question_number}")

GAMES/Quizzer/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

GAMES/Quizzer/quiz_brain.py

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

GAMES/Quizzer/ui.py

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

0 commit comments

Comments
 (0)