From d23a9c9503a06d2e0f6d425b54029fbd08f0c68a Mon Sep 17 00:00:00 2001 From: Tanguy Deleplanque Date: Fri, 27 Jun 2025 12:00:57 +0200 Subject: [PATCH] day 17 --- 017/data.py | 14 ++++++++++++++ 017/main.py | 19 +++++++++++++++++++ 017/question_model.py | 4 ++++ 017/quiz_brain.py | 25 +++++++++++++++++++++++++ 4 files changed, 62 insertions(+) create mode 100644 017/data.py create mode 100644 017/main.py create mode 100644 017/question_model.py create mode 100644 017/quiz_brain.py diff --git a/017/data.py b/017/data.py new file mode 100644 index 0000000..7ef0e8b --- /dev/null +++ b/017/data.py @@ -0,0 +1,14 @@ +question_data = [ + {"text": "A slug's blood is green.", "answer": "True"}, + {"text": "The loudest animal is the African Elephant.", "answer": "False"}, + {"text": "Approximately one quarter of human bones are in the feet.", "answer": "True"}, + {"text": "The total surface area of a human lungs is the size of a football pitch.", "answer": "True"}, + {"text": "In West Virginia, USA, if you accidentally hit an animal with your car, you are free to take it home to eat.", "answer": "True"}, + {"text": "In London, UK, if you happen to die in the House of Parliament, you are entitled to a state funeral.", "answer": "False"}, + {"text": "It is illegal to pee in the Ocean in Portugal.", "answer": "True"}, + {"text": "You can lead a cow down stairs but not up stairs.", "answer": "False"}, + {"text": "Google was originally called 'Backrub'.", "answer": "True"}, + {"text": "Buzz Aldrin's mother's maiden name was 'Moon'.", "answer": "True"}, + {"text": "No piece of square dry paper can be folded in half more than 7 times.", "answer": "False"}, + {"text": "A few ounces of chocolate can to kill a small dog.", "answer": "True"}, +] \ No newline at end of file diff --git a/017/main.py b/017/main.py new file mode 100644 index 0000000..b1c424e --- /dev/null +++ b/017/main.py @@ -0,0 +1,19 @@ +from question_model import Question +from quiz_brain import QuizBrain +import httpx + +r = httpx.get("https://opentdb.com/api.php?amount=20&difficulty=easy&type=boolean") + +question_data = r.json() + +question_bank = [] +for a_question in question_data['results']: + question_bank.append(Question(a_question['question'], a_question['correct_answer'])) + +quiz = QuizBrain(question_bank) + +while quiz.still_has_question(): + quiz.next_question() + +print("You've completed the quiz") +print(f"You final score was {quiz.score}/{quiz.question_number}") \ No newline at end of file diff --git a/017/question_model.py b/017/question_model.py new file mode 100644 index 0000000..5b09db6 --- /dev/null +++ b/017/question_model.py @@ -0,0 +1,4 @@ +class Question: + def __init__(self, text, answer): + self.text = text + self.answer = answer \ No newline at end of file diff --git a/017/quiz_brain.py b/017/quiz_brain.py new file mode 100644 index 0000000..d8a3d54 --- /dev/null +++ b/017/quiz_brain.py @@ -0,0 +1,25 @@ +class QuizBrain: + def __init__(self, questions_list): + self.question_number = 0 + self.questions_list = questions_list + self.score = 0 + + def still_has_question(self): + return self.question_number < len(self.questions_list) + + def next_question(self): + current_question = self.questions_list[self.question_number] + self.question_number += 1 + user_answer = input(f"Q.{self.question_number}: {current_question.text} (True/False)?: ") + + is_correct = self.check_answer(user_answer, current_question.answer) + + def check_answer(self, user_answer, expected_answer): + if user_answer.lower() == expected_answer.lower(): + print("You got it right!") + self.score +=1 + else: + print("That's wrong") + print(f"The correct answer was: {expected_answer}") + print(f"You current score is {self.score} / {self.question_number}") + print("\n") \ No newline at end of file