|
| 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) |
0 commit comments