-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 990f832
Showing
11 changed files
with
125 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import requests, json | ||
|
||
para = { | ||
"amount": 10, | ||
"type": "boolean", | ||
} | ||
|
||
response = requests.get(url="https://opentdb.com/api.php", params=para) | ||
response.raise_for_status() | ||
|
||
data = response.json() | ||
question_data = (data['results']) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
quiz_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}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
from tkinter import * | ||
from quiz_brain import QuizBrain | ||
|
||
THEME_COLOR = "#375362" | ||
|
||
class QuizInterface: | ||
def __init__(self, quiz_brain: QuizBrain): | ||
self.quiz = quiz_brain | ||
self.window = Tk() | ||
self.window.title("Quizzler") | ||
self.window.config(padx=20, pady=20, background=THEME_COLOR) | ||
self.score_lbl = Label(text="Score: 0", fg="white", bg=THEME_COLOR) | ||
self.score_lbl.grid(row=0,column=1) | ||
|
||
self.canvas = Canvas(width=300, height=250, bg="white") | ||
self.question_text = self.canvas.create_text(150, 125, width=275, text="some text", font=("Arial", 10, "italic"), fill=THEME_COLOR) | ||
self.canvas.grid(row=1, column=0, columnspan=2, pady=50) | ||
|
||
false_img = PhotoImage(file="./images/false.png") | ||
true_img = PhotoImage(file="./images/true.png") | ||
self.true_button = Button(image=true_img, highlightthickness=0,command=self.answer_true) | ||
self.true_button.grid(row=2, column=0, padx=20, pady=20) | ||
self.false_button = Button(image=false_img, highlightthickness=0,command=self.answer_false) | ||
self.false_button.grid(row=2, column=1, padx=20, pady=20) | ||
|
||
self.get_next_question() | ||
|
||
|
||
self.window.mainloop() | ||
|
||
def get_next_question(self): | ||
self.canvas.configure(bg="white") | ||
|
||
if self.quiz.still_has_questions(): | ||
self.score_lbl.config(text=f"Score: {self.quiz.score}") | ||
q_text = self.quiz.next_question() | ||
self.canvas.itemconfig(self.question_text, text=q_text) | ||
else: | ||
self.canvas.itemconfig(self.question_text, text="THE END!") | ||
self.true_button.config(state="disabled") | ||
self.false_button.config(state="disabled") | ||
|
||
def answer_true(self): | ||
self.give_feedback(self.quiz.check_answer("True")) | ||
|
||
def answer_false(self): | ||
self.give_feedback(self.quiz.check_answer("False")) | ||
|
||
def give_feedback(self, is_right): | ||
if is_right: | ||
self.canvas.configure(bg="green") | ||
else: | ||
self.canvas.configure(bg="red") | ||
|
||
self.window.after(1000, self.get_next_question) | ||
|