Skip to content

Commit

Permalink
complete
Browse files Browse the repository at this point in the history
  • Loading branch information
clustermaster99 committed Jul 21, 2023
0 parents commit 990f832
Show file tree
Hide file tree
Showing 11 changed files with 125 additions and 0 deletions.
Binary file added __pycache__/data.cpython-39.pyc
Binary file not shown.
Binary file added __pycache__/question_model.cpython-39.pyc
Binary file not shown.
Binary file added __pycache__/quiz_brain.cpython-39.pyc
Binary file not shown.
Binary file added __pycache__/ui.cpython-39.pyc
Binary file not shown.
12 changes: 12 additions & 0 deletions data.py
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'])
Binary file added 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 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 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)
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}")
5 changes: 5 additions & 0 deletions 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
31 changes: 31 additions & 0 deletions quiz_brain.py
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")
56 changes: 56 additions & 0 deletions ui.py
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)

0 comments on commit 990f832

Please sign in to comment.