import requests
parameters = {
"amount": 10,
"type": "boolean"
}
response = requests.get(url="https://opentdb.com/api.php", params=parameters)
response.raise_for_status()
data = response.json()
question_data = data["results"]
params=
로 입력안하고 https://opentdb.com/api.php?amount=10&type=boolean
로 입력해도 똑같이 출력되긴 하는데 params=
로 입력하는 습관이 필요할것 같음
https://www.w3schools.com/html/html_entities.asp
quiz_brain.py
import html
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)
user_answer = input(f"Q.{self.question_number}: {q_text} (True/False): ")
self.check_answer(user_answer)
.unescape()
in html
module
"
이 "
, '
이 '
등으로 보이는 것을 변경해줌
def greetings(name: str) -> str:
return 'Hello ' + name
This is known as a type hint in Python.
And it is feature that we got relatively recently from Python and it has a lot of benefits especially when we want our IDE to help us spot potential bugs and keep our code safer and spend less time debugging and more time writing code.
(name: str)
: declare a data type for a variable
->
: specify the data type of the output of a function.
age: int
name: str
height: float
is_human: bool
def police_check(age: int) -> bool:
if age > 18:
can_drive = True
else:
can_drive = False
return can_drive
if police_check(12):
print("You may pass")
else:
print("Pay a fine.")
ui.py
from tkinter import *
THEME_COLOR = "#375362"
class QuizInterface:
def __init__(self):
self.window = Tk()
self.window.title("Quizzler")
self.window.config(padx=20, pady=20, bg=THEME_COLOR)
self.score_label = Label(text="Score: 0", fg="white", bg=THEME_COLOR)
self.score_label.grid(column=1, row=0)
self.canvas = Canvas(height=250, width=300, bg="white")
self.question_text = self.canvas.create_text(
150,
125,
text="Text",
fill=THEME_COLOR,
font=("Arial", 20, "italic")
)
self.canvas.grid(column=0, row=1, columnspan=2, pady=50)
true_image = PhotoImage(file="images/true.png")
self.true_button = Button(image=true_image, highlightthickness=0)
self.true_button.grid(column=0, row=2)
false_image = PhotoImage(file="images/false.png")
self.false_button = Button(image=false_image, highlightthickness=0)
self.false_button.grid(column=1, row=2)
self.window.mainloop()
ui.py
from tkinter import *
from quiz_brain import QuizBrain
# We initialize a new quiz interface, we must pass in a quiz brain object which is of the data type quiz brain.
THEME_COLOR = "#375362"
class QuizInterface:
def __init__(self, quiz_brain: QuizBrain): # add the datatype "QuizBrain"
self.quiz = quiz_brain
...
self.canvas = Canvas(height=250, width=300, bg="white")
self.question_text = self.canvas.create_text(
150,
125,
width=280,
text="Text",
fill=THEME_COLOR,
font=("Arial", 20, "italic")
)
self.canvas.grid(column=0, row=1, columnspan=2, pady=50)
...
self.get_next_question()
self.window.mainloop()
def get_next_question(self):
q_text = self.quiz.next_question()
self.canvas.itemconfig(self.question_text, text=q_text)
quiz_brain.py
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
ui.py
...
self.true_button = Button(image=true_image, highlightthickness=0, command=self.true_pressed)
...
self.false_button = Button(image=false_image, highlightthickness=0, command=self.false_pressed)
...
def true_pressed(self):
self.quiz.check_answer("True")
def false_pressed(self):
self.quiz.check_answer("False")
def get_next_question(self):
self.canvas.config(bg="white")
if self.quiz.still_has_questions():
self.score_label.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="You've reached the end of the quiz.")
self.true_button.config(state="disabled")
self.false_button.config(state="disabled")
def true_pressed(self):
self.give_feedback(self.quiz.check_answer("True"))
def false_pressed(self):
is_right = self.quiz.check_answer("False")
self.give_feedback(is_right)
def give_feedback(self, is_right):
if is_right:
self.canvas.config(bg="green")
else:
self.canvas.config(bg="red")
self.window.after(1000, self.get_next_question)
true_pressed()
의 한 줄과 false_pressed()
의 두줄은 (출력값 제외) 같음.
Button.config(state="disabled")
는 더이상 버튼이 작동하지 않도록 함.
give_feedback()
은 is_right
값을 확인 후 bg컬러 변경하고 1초 뒤에 get_next_question
을 수행함.
ui.py
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, bg=THEME_COLOR)
self.score_label = Label(text="Score: 0", fg="white", bg=THEME_COLOR)
self.score_label.grid(column=1, row=0)
self.canvas = Canvas(height=250, width=300, bg="white")
self.question_text = self.canvas.create_text(
150,
125,
width=280,
text="Text",
fill=THEME_COLOR,
font=("Arial", 20, "italic")
)
self.canvas.grid(column=0, row=1, columnspan=2, pady=50)
true_image = PhotoImage(file="images/true.png")
self.true_button = Button(image=true_image, highlightthickness=0, command=self.true_pressed)
self.true_button.grid(column=0, row=2)
false_image = PhotoImage(file="images/false.png")
self.false_button = Button(image=false_image, highlightthickness=0, command=self.false_pressed)
self.false_button.grid(column=1, row=2)
self.get_next_question()
self.window.mainloop()
def get_next_question(self):
self.canvas.config(bg="white")
if self.quiz.still_has_questions():
self.score_label.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="You've reached the end of the quiz.")
self.true_button.config(state="disabled")
self.false_button.config(state="disabled")
def true_pressed(self):
self.give_feedback(self.quiz.check_answer("True"))
def false_pressed(self):
is_right = self.quiz.check_answer("False")
self.give_feedback(is_right)
def give_feedback(self, is_right):
if is_right:
self.canvas.config(bg="green")
else:
self.canvas.config(bg="red")
self.window.after(1000, self.get_next_question)
main.py
from question_model import Question
from data import question_data
from ui import *
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}")
뭔가 되는 듯.. 안되는 듯...