diff --git a/GAMES/Quizzer/README.md b/GAMES/Quizzer/README.md new file mode 100644 index 00000000..27cf5b1d --- /dev/null +++ b/GAMES/Quizzer/README.md @@ -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) diff --git a/GAMES/Quizzer/Sample_Output.png b/GAMES/Quizzer/Sample_Output.png new file mode 100644 index 00000000..58e849e6 Binary files /dev/null and b/GAMES/Quizzer/Sample_Output.png differ diff --git a/GAMES/Quizzer/data.py b/GAMES/Quizzer/data.py new file mode 100644 index 00000000..812fbae0 --- /dev/null +++ b/GAMES/Quizzer/data.py @@ -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"] + + + + + + diff --git a/GAMES/Quizzer/images/false.png b/GAMES/Quizzer/images/false.png new file mode 100644 index 00000000..904c76d8 Binary files /dev/null and b/GAMES/Quizzer/images/false.png differ diff --git a/GAMES/Quizzer/images/true.png b/GAMES/Quizzer/images/true.png new file mode 100644 index 00000000..79ca9a30 Binary files /dev/null and b/GAMES/Quizzer/images/true.png differ diff --git a/GAMES/Quizzer/main.py b/GAMES/Quizzer/main.py new file mode 100644 index 00000000..9715f1df --- /dev/null +++ b/GAMES/Quizzer/main.py @@ -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}") diff --git a/GAMES/Quizzer/question_model.py b/GAMES/Quizzer/question_model.py new file mode 100644 index 00000000..b3d63d36 --- /dev/null +++ b/GAMES/Quizzer/question_model.py @@ -0,0 +1,5 @@ +class Question: + + def __init__(self, q_text, q_answer): + self.text = q_text + self.answer = q_answer diff --git a/GAMES/Quizzer/quiz_brain.py b/GAMES/Quizzer/quiz_brain.py new file mode 100644 index 00000000..8c8db11f --- /dev/null +++ b/GAMES/Quizzer/quiz_brain.py @@ -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") + diff --git a/GAMES/Quizzer/ui.py b/GAMES/Quizzer/ui.py new file mode 100644 index 00000000..da692f1b --- /dev/null +++ b/GAMES/Quizzer/ui.py @@ -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)