Skip to content

Quizzer #222

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions GAMES/Quizzer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Quizzer

### 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

## About the Quiz Topics:
- The topic of the quiz is **Anime/Manga** by default.
- You can change it by changing the API parameters:
1. Opening *Quizzer/data.py* in your editor.
2. You can change *category* to "Computer_Science" or "GK"
3. To get an all-in-one quiz remove *category*

## How to Run:
- Open terminal and navigate to the file

```
python main.py
```


## Sample Output
![Sample Output](https://github.com/sahil-s-246/Python-project-Scripts/blob/sahil-s-246-patch-2/GAMES/Quizzer/Sample_Output.png)
Binary file added GAMES/Quizzer/Sample_Output.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions GAMES/Quizzer/data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import requests


# Index
# GK : 9
# Computer Science: 18
# Anime / Manga : 31
# to get all topics in one quiz , remove category from parameters
NUM_OF_QUESTIONS = 20
category = 31

parameters = {
"category": category,
"amount": NUM_OF_QUESTIONS,
"type": "boolean"
}

response = requests.get("https://opentdb.com/api.php", params=parameters)
question_data = response.json()["results"]






Binary file added GAMES/Quizzer/images/false.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added GAMES/Quizzer/images/true.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions GAMES/Quizzer/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from question_model import Question
from data import question_data
from quiz_brain import QuizBrain
from ui import QuizInterface

question_bank = []
for question in question_data:
question_text = question["question"]
question_answer = question["correct_answer"]
new_question = Question(question_text, question_answer)
question_bank.append(new_question)


quiz = QuizBrain(question_bank)
ui = QuizInterface(quiz)

while quiz.still_has_questions():
quiz.next_question()

print("You've completed the quiz")
print(f"Your final score was: {quiz.score}/{quiz.question_number}")
5 changes: 5 additions & 0 deletions GAMES/Quizzer/question_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Question:

def __init__(self, q_text, q_answer):
self.text = q_text
self.answer = q_answer
32 changes: 32 additions & 0 deletions GAMES/Quizzer/quiz_brain.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import html


class QuizBrain:
def __init__(self, q_list):
self.question_number = 0
self.score = 0
self.question_list = q_list
self.current_question = None

def still_has_questions(self):
return self.question_number < len(self.question_list)

def next_question(self):
self.current_question = self.question_list[self.question_number]
self.question_number += 1
q_text = html.unescape(self.current_question.text)
return f"Q.{self.question_number}: {q_text}"
# user_answer = input(f"Q.{self.question_number}: {q_text} (True/False): ")
# self.check_answer(user_answer)

def check_answer(self, user_answer):
correct_answer = self.current_question.answer
if user_answer.lower() == correct_answer.lower():
self.score += 1
return True
else:
return False

# print(f"Your current score is: {self.score}/{self.question_number}")
# print("\n")

61 changes: 61 additions & 0 deletions GAMES/Quizzer/ui.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
from tkinter import *
from quiz_brain import QuizBrain

THEME_COLOR = "#375362"


class QuizInterface(Tk):

def __init__(self, quiz_brain: QuizBrain):
super().__init__()
self.quiz = quiz_brain
self.title('Quizzer')
self.config(bg=THEME_COLOR, padx=20, pady=20)

self.label = Label(self, text="Score: 0", bg=THEME_COLOR, foreground="white")
self.label.grid(row=0, column=1)

self.canvas = Canvas(self, width=300, height=250, bg="white")
self.question_text = self.canvas.create_text(150, 125,
text="",
fill=THEME_COLOR,
font=("Arial", 15, "italic"),
width=280)
self.canvas.grid(row=1, column=0, columnspan=2, pady=50)

true_img = PhotoImage(file="images/true.png")
self.true = Button(self, image=true_img, command=self.true_press, highlightthickness=0)
self.true.grid(row=2, column=0)
false_img = PhotoImage(file="images/false.png")
self.false = Button(self, image=false_img, command=self.false_press, highlightthickness=0)
self.false.grid(row=2, column=1)

self.question_box()
self.mainloop()

def question_box(self):
self.canvas.config(bg="white")
if self.quiz.still_has_questions():

q_text = self.quiz.next_question()
self.canvas.itemconfig(self.question_text, text=q_text)

else:
self.canvas.itemconfig(self.question_text, text="You have reached the end of the quiz!")
self.true.destroy()
self.false.destroy()

def true_press(self):
self.give_feedback(self.quiz.check_answer("True"))

def false_press(self):
self.give_feedback(self.quiz.check_answer("False"))

def give_feedback(self, is_right):
if is_right:
self.label.config(text=f"Score: {self.quiz.score}")
self.canvas.config(bg="green")
else:
self.canvas.config(bg="red")

self.after(1000, self.question_box)