|
1 |
| -# Your code goes here. |
2 |
| -# You can delete these comments, but do not change the name of this file |
3 |
| -# Write your code to expect a terminal of 80 characters wide and 24 rows high |
| 1 | +import requests |
| 2 | +import json |
| 3 | +import random |
| 4 | +from pprint import pprint |
| 5 | + |
| 6 | +categories = json.loads(requests.get( |
| 7 | + "https://opentdb.com/api_category.php").text)["trivia_categories"] |
| 8 | + |
| 9 | + |
| 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 | + """ |
| 36 | + |
| 37 | + def __init__(self, num_qs, category, difficulty): |
| 38 | + |
| 39 | + self.num_qs = num_qs |
| 40 | + self.category = category |
| 41 | + self.difficulty = difficulty |
| 42 | + |
| 43 | + self.question_data = get_quiz_questions(num_qs, category, difficulty) |
| 44 | + |
| 45 | + self.questions_list = [Question(**question) |
| 46 | + for question in self.question_data] |
| 47 | + |
| 48 | + |
| 49 | +class Question: |
| 50 | + """ |
| 51 | + Creates a Quiz Question instance |
| 52 | + """ |
| 53 | + |
| 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(): |
| 77 | + |
| 78 | + print("Welcome to QuizMaster 2022! \n") |
| 79 | + |
| 80 | + round_1 = Round(5, 9, "easy") |
| 81 | + |
| 82 | + |
| 83 | +main() |
0 commit comments