Skip to content

Commit a3dd4f8

Browse files
committed
Split Quiz logic into own file quiz.py
1 parent 463d5ab commit a3dd4f8

File tree

3 files changed

+151
-71
lines changed

3 files changed

+151
-71
lines changed

.vscode/settings.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
2-
"python.linting.pylintEnabled": true,
2+
"python.linting.pylintEnabled": false,
33
"python.linting.enabled": true,
4-
"python.linting.pycodestyleEnabled": false,
5-
"python.linting.flake8Enabled": true,
4+
"python.linting.pycodestyleEnabled": true,
5+
"python.linting.flake8Enabled": false,
66
"python.terminal.activateEnvironment": false,
77
"python.formatting.autopep8Path": "/home/gitpod/.pyenv/shims/autopep8",
88
"python.linting.flake8Path": "/home/gitpod/.pyenv/shims/flake8",

quiz.py

+128
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
import requests
2+
import json
3+
import random
4+
from pprint import pprint
5+
6+
ALL_CATEGORIES = json.loads(requests.get(
7+
"https://opentdb.com/api_category.php").text)["trivia_categories"]
8+
9+
SESSION_TOKEN = json.loads(requests.get(
10+
"https://opentdb.com/api_token.php?command=request").text)["token"]
11+
12+
13+
def get_quiz_questions(num_qs, cat, diff):
14+
"""
15+
Makes a call to the Quiz API and returns a list of
16+
multiple choice questions with category, type, difficulty,
17+
question, correct_answer and incorrect_answers
18+
"""
19+
20+
return json.loads(requests.get(
21+
f"https://opentdb.com/api.php?amount={num_qs}&category={cat}&difficulty={diff}&type=multiple&token={SESSION_TOKEN}").text)["results"] # noqa
22+
23+
24+
class Game:
25+
"""
26+
Generates a Quiz Game,
27+
Expects 5 parameters: Quiz Title, Number of Rounds,
28+
Number of Questions in a round, a list of the categories
29+
for each round, and the difficulty
30+
"""
31+
32+
def __init__(self, quiz_title, num_rounds, num_qs, categories, difficulty):
33+
self.quiz_title = quiz_title
34+
self.num_rounds = num_rounds
35+
self.num_questions = num_qs
36+
self.categories = categories
37+
self.difficulty = difficulty
38+
39+
self.rounds = [Round(self.num_questions, self.categories[x],
40+
self.difficulty) for x in range(self.num_rounds)]
41+
print(self.rounds)
42+
43+
for round in self.rounds:
44+
print(round.__dict__)
45+
46+
47+
class Round:
48+
"""
49+
Generates a Quiz Round
50+
Expects three parameters: Num of Questions,
51+
Category, and Difficulty
52+
"""
53+
54+
def __init__(self, num_qs, category, difficulty):
55+
56+
self.num_qs = num_qs
57+
self.category = category
58+
self.difficulty = difficulty
59+
60+
self.question_data = get_quiz_questions(num_qs, category, difficulty)
61+
62+
self.questions_list = [Question(**question)
63+
for question in self.question_data]
64+
65+
66+
class Question:
67+
"""
68+
Creates a Quiz Question instance
69+
"""
70+
71+
def __init__(
72+
self, category, type, difficulty, question,
73+
correct_answer, incorrect_answers):
74+
75+
self.category = category
76+
self.qtype = type
77+
self.difficulty = difficulty
78+
self.question = question
79+
self.correct_answer = correct_answer
80+
self.incorrect_answers = incorrect_answers
81+
82+
def ask(self):
83+
return self.question
84+
85+
86+
def get_quiz_categories(categories):
87+
88+
print_cats = ""
89+
for x in range(len(categories)):
90+
print_cats += (f"{categories[x]['id']}: {categories[x]['name']} \n")
91+
92+
return print_cats
93+
94+
95+
def setup_new_quiz():
96+
"""
97+
Asks user for inputs and creates a new quiz based on the inputs
98+
"""
99+
100+
# ADD VALIDATION TO INPUTS
101+
102+
# Add loops to ensure all inputs are completed
103+
104+
title = input("What is the name of this Quiz? \n")
105+
rounds = int(input("How many rounds should the quiz have? \n"))
106+
q_num = int(input("How many questions in each round? \n"))
107+
108+
# Possibly ask the categories for each round one by one and
109+
# add to a list?
110+
111+
print(get_quiz_categories(ALL_CATEGORIES))
112+
113+
cats = []
114+
for x in range(1, rounds+1):
115+
cat = int(input(f"Chose a category for round {x}: \n"))
116+
cats.append(cat)
117+
118+
# cats = [int(x) for x in input(
119+
# f"Chose {rounds} categories from the list, "
120+
# "separated by commas \n").split(",")]
121+
122+
print(cats)
123+
124+
diff = input(
125+
"What difficulty should the questions be? \n"
126+
"Easy, Medium or Hard? \n").lower()
127+
128+
return Game(title, rounds, q_num, cats, diff)

run.py

+20-68
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,35 @@
1-
import requests
2-
import json
3-
import random
4-
from pprint import pprint
1+
import quiz
2+
from datetime import datetime
53

6-
categories = json.loads(requests.get(
7-
"https://opentdb.com/api_category.php").text)["trivia_categories"]
4+
now = datetime.now()
85

6+
current_hour = int(now.strftime("%H"))
97

10-
def get_quiz_questions(num_qs, cat, diff):
11-
"""
12-
Makes a call to the Quiz API and returns a list of
13-
multiple choice questions with category, type, difficulty,
14-
question, correct_answer and incorrect_answers
15-
"""
16-
return json.loads(requests.get(
17-
f"https://opentdb.com/api.php?amount={num_qs}&category={cat}&difficulty={diff}&type=multiple").text)["results"]
18-
19-
20-
class Game:
21-
"""
22-
Generates a Quiz Game of several Rounds
23-
"""
24-
25-
def __init__(self, quiz_title, num_rounds):
26-
self.quiz_title = quiz_title
27-
self.num_rounds = num_rounds
28-
29-
30-
class Round:
31-
"""
32-
Generates a Quiz Round
33-
Expects three parameters: Num of Questions,
34-
Category, and Difficulty
35-
"""
368

37-
def __init__(self, num_qs, category, difficulty):
9+
def pick_greeting(hour):
10+
if hour < 6:
11+
return "Wow, you're up late!"
12+
elif hour < 12:
13+
return "Good morning!"
14+
elif hour < 17:
15+
return "Good afternoon."
16+
else:
17+
return "Good evening."
3818

39-
self.num_qs = num_qs
40-
self.category = category
41-
self.difficulty = difficulty
4219

43-
self.question_data = get_quiz_questions(num_qs, category, difficulty)
20+
greeting = pick_greeting(current_hour)
4421

45-
self.questions_list = [Question(**question)
46-
for question in self.question_data]
4722

48-
49-
class Question:
23+
def main():
5024
"""
51-
Creates a Quiz Question instance
25+
Code to run on terminal boot
5226
"""
5327

54-
def __init__(self, category, type, difficulty, question, correct_answer, incorrect_answers):
55-
56-
self.category = category
57-
self.qtype = type
58-
self.difficulty = difficulty
59-
self.question = question
60-
self.correct_answer = correct_answer
61-
self.incorrect_answers = incorrect_answers
62-
63-
def ask(self):
64-
return self.question
65-
66-
67-
def get_quiz_categories(categories):
68-
69-
print_cats = ""
70-
for x in range(len(categories)):
71-
print_cats += (f"{categories[x]['id']}: {categories[x]['name']} \n")
72-
73-
return print_cats
74-
75-
76-
def main():
28+
print(f"{greeting} Welcome to QuizMaster 2022! \n")
7729

78-
print("Welcome to QuizMaster 2022! \n")
30+
game_1 = quiz.setup_new_quiz()
7931

80-
round_1 = Round(5, 9, "easy")
32+
print(game_1)
8133

8234

83-
main()
35+
main()

0 commit comments

Comments
 (0)