Skip to content

Commit 821eeca

Browse files
committed
Day 35
- Keys, Authentication & Environment Variables: Send SMS - major changes in day034 project
1 parent 40a1ee7 commit 821eeca

File tree

9 files changed

+290
-85
lines changed

9 files changed

+290
-85
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
config.env
12
__pycache__/
23
*.py[cod]
3-
*$py.class
4+
*$py.class
5+
.DS_Store

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
- [Day 32](https://github.com/a092devs/100-days-of-python/tree/master/day032) - Send Email (smtplib) & Manage Dates (datetime) - Automated Birthday Wisher
4141
- [Day 33](https://github.com/a092devs/100-days-of-python/tree/master/day033) - API Endpoints & API Parameters - ISS Overhead Notifier
4242
- [Day 34](https://github.com/a092devs/100-days-of-python/tree/master/day034) - API Practice - Creating a GUI Quiz App
43+
- [Day 35](https://github.com/a092devs/100-days-of-python/tree/master/day035) - Keys, Authentication & Environment Variables: Send SMS
4344

4445
## ⚙ Tools and Technologies Covered
4546
- Python 3
@@ -52,4 +53,3 @@
5253
- Python GUI Desktop App Development
5354
- Tkinter
5455
- Pyinstaller
55-
- Pandas

day034/data.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
import requests
2+
from question_model import Question
23

3-
paramaters = {
4-
"amount" : 10,
5-
"type" : "boolean"
6-
}
4+
URL = 'https://opentdb.com/api.php'
75

8-
resp = requests.get("https://opentdb.com/api.php", paramaters)
9-
resp.raise_for_status()
10-
data = resp.json()
11-
question_data = data["results"]
6+
def quiz_data(amount, category, difficulty):
7+
parameters = {
8+
"amount": int(amount),
9+
"category": category,
10+
"difficulty": difficulty.lower(),
11+
"type": "boolean"
12+
}
13+
14+
if category == 0:
15+
del parameters["category"]
16+
if difficulty.lower() == "any difficulty":
17+
del parameters["difficulty"]
18+
question_response = requests.get(URL, params=parameters)
19+
question_response.raise_for_status()
20+
temp = question_response.json()
21+
question_data = temp["results"]
22+
return [Question(q["question"], q["correct_answer"]) for q in question_data]

day034/main.py

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,5 @@
1-
from question_model import Question
2-
from data import question_data
31
from quiz_brain import QuizBrain
42
from ui import QuizInterface
53

6-
question_bank = []
7-
for question in question_data:
8-
question_text = question["question"]
9-
question_answer = question["correct_answer"]
10-
new_question = Question(question_text, question_answer)
11-
question_bank.append(new_question)
12-
13-
quiz = QuizBrain(question_bank)
14-
quiz_ui = QuizInterface(quiz)
15-
16-
print("You've completed the quiz")
17-
print(f"Your final score was: {quiz.score}/{quiz.question_number}")
4+
quiz = QuizBrain()
5+
quiz_ui = QuizInterface(quiz)

day034/question_model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
class Question:
22
def __init__(self, q_text, q_answer):
33
self.text = q_text
4-
self.answer = q_answer
4+
self.answer = q_answer

day034/quiz_brain.py

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,46 @@
11
import html
2+
from data import quiz_data
3+
4+
AMOUNT = 10
5+
CATEGORY = 0
6+
DIFFICULTY = "easy"
27

38
class QuizBrain:
4-
def __init__(self, q_list):
5-
self.question_number = 0
9+
def __init__(self):
10+
self.question_list = None
11+
self.question_number = None
12+
self.score = None
13+
self.category = CATEGORY
14+
self.difficulty = DIFFICULTY
15+
self.num_questions = AMOUNT
16+
params = {
17+
"category": self.category,
18+
"difficulty": self.difficulty,
19+
"amount": self.num_questions
20+
}
21+
self.setup(params)
22+
23+
def setup(self, params):
24+
self.category = params["category"]
25+
self.difficulty = params["difficulty"]
26+
self.num_questions = params["amount"]
27+
self.question_list = quiz_data(self.num_questions, self.category, self.difficulty)
628
self.score = 0
7-
self.question_list = q_list
8-
self.current_question = None
29+
self.question_number = 0
930

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

1334
def next_question(self):
14-
self.current_question = self.question_list[self.question_number]
35+
question = self.question_list[self.question_number]
36+
question_text = html.unescape(question.text)
1537
self.question_number += 1
16-
q_text = html.unescape(self.current_question.text)
17-
return f"Q.{self.question_number}: {q_text}"
38+
return f'Q.{self.question_number}: {question_text} (True/False)?'
1839

19-
def check_answer(self, user_answer):
20-
correct_answer = self.current_question.answer
21-
if user_answer.lower() != correct_answer.lower():
40+
def check_answer(self, u_answer):
41+
correct_answer = self.question_list[self.question_number-1].answer
42+
if correct_answer.lower() != u_answer.lower():
2243
return False
2344
self.score += 1
24-
return True
45+
return True
46+

day034/setup.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import tkinter as tk
2+
3+
THEME_COLOR = "#375362"
4+
5+
CATEGORIES = [
6+
"Any category",
7+
"Science & Nature",
8+
"Science: Computers",
9+
"Science: Mathematics",
10+
"Mythology",
11+
"Geography",
12+
"History",
13+
"Politics",
14+
"Animals",
15+
"Vehicles"
16+
]
17+
18+
CATEGORY_IDS = [
19+
0,
20+
17,
21+
18,
22+
19,
23+
20,
24+
22,
25+
23,
26+
24,
27+
27,
28+
28
29+
]
30+
31+
DIFFICULTY_LEVELS = [
32+
"Any difficulty",
33+
"Easy",
34+
"Medium",
35+
"Hard"
36+
]
37+
38+
39+
class SetupUI:
40+
def __init__(self, main_ui, selected_params):
41+
self.main_ui = main_ui
42+
43+
self.window = tk.Toplevel()
44+
self.window.title("Quizzer: Setup")
45+
self.window.config(padx=20, pady=20, bg=THEME_COLOR)
46+
47+
category_label = tk.Label(self.window, text="Category:", anchor='e', justify='left',
48+
bg=THEME_COLOR, fg="white")
49+
category_label.grid(row=0, column=0, sticky="W", padx=5, pady=5)
50+
self.category = tk.StringVar()
51+
self.category.set(CATEGORIES[CATEGORY_IDS.index(selected_params["category"])])
52+
self.category_dropdown = tk.OptionMenu(self.window, self.category, *CATEGORIES)
53+
self.category_dropdown.config(width=20, bg=THEME_COLOR, fg="white", highlightthickness=0)
54+
self.category_dropdown.grid(row=0, column=1, padx=5, pady=5, sticky="EW")
55+
56+
difficulty_label = tk.Label(self.window, text="Difficulty:", anchor='e',
57+
justify='left', bg=THEME_COLOR, fg="white")
58+
difficulty_label.grid(row=1, column=0, sticky="W", padx=5, pady=5)
59+
self.difficulty = tk.StringVar()
60+
self.difficulty.set(selected_params["difficulty"].capitalize())
61+
self.difficulty_dropdown = tk.OptionMenu(self.window, self.difficulty, *DIFFICULTY_LEVELS)
62+
self.difficulty_dropdown.config(bg=THEME_COLOR, fg="white", highlightthickness=0)
63+
self.difficulty_dropdown.grid(row=1, column=1, sticky="EW", padx=5, pady=5)
64+
65+
num_questions_label = tk.Label(self.window, text="Number of questions:", anchor='e',
66+
justify='left', bg=THEME_COLOR, fg="white")
67+
num_questions_label.grid(row=2, column=0, sticky="W", padx=5, pady=5)
68+
self.num_questions_edit = tk.Entry(self.window)
69+
self.num_questions_edit.config(width=26, bg=THEME_COLOR, fg="white")
70+
self.num_questions_edit.insert(0, selected_params["num_questions"])
71+
self.num_questions_edit.grid(row=2, column=1, sticky="EW", padx=5, pady=5)
72+
73+
self.ok_button = tk.Button(self.window, text="OK", command=self.pass_selections)
74+
self.ok_button.config(width=22, bg=THEME_COLOR, fg="white")
75+
self.ok_button.grid(row=3, column=1, sticky="EW", padx=5, pady=5)
76+
77+
self.cancel_button = tk.Button(self.window, text="Cancel", command=self.cancel)
78+
self.cancel_button.config(width=22, bg=THEME_COLOR, fg="white")
79+
self.cancel_button.grid(row=3, column=0, sticky="EW", padx=5, pady=5)
80+
81+
self.window.mainloop()
82+
83+
def pass_selections(self):
84+
category = self.category.get()
85+
category_id = CATEGORY_IDS[CATEGORIES.index(category)]
86+
difficulty = self.difficulty.get()
87+
num_questions = self.num_questions_edit.get()
88+
89+
self.main_ui.setup_quiz(category_id, difficulty, num_questions)
90+
self.window.destroy()
91+
92+
def cancel(self):
93+
self.window.destroy()

0 commit comments

Comments
 (0)