diff --git a/projects/Quiz Game/issue-734.diff b/projects/Quiz Game/issue-734.diff new file mode 100644 index 00000000..e60c1903 Binary files /dev/null and b/projects/Quiz Game/issue-734.diff differ diff --git a/projects/Quiz Game/src/__init__.py b/projects/Quiz Game/src/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/projects/Quiz Game/src/gameplay.py b/projects/Quiz Game/src/gameplay.py new file mode 100644 index 00000000..a4fa8f9f --- /dev/null +++ b/projects/Quiz Game/src/gameplay.py @@ -0,0 +1,18 @@ +from gamesystem import QuizGameSystem + +def main(): + game = QuizGameSystem() + game.welcome_message() + if(game.start_game()): + score = 0 + total_questions = len(game.get_questions()) + + for question in game.get_questions(): + if (game.ask_question(question["question"], question["answer"])): + score += 1 + + game.show_result(score, total_questions) + + +if __name__ == "__main__": + main() diff --git a/projects/Quiz Game/src/gamesystem.py b/projects/Quiz Game/src/gamesystem.py new file mode 100644 index 00000000..371c0d34 --- /dev/null +++ b/projects/Quiz Game/src/gamesystem.py @@ -0,0 +1,96 @@ +class QuizGameSystem: + def __init__(self): + ''' + Initialize the game with instance + Set the object by calling the get_class method + ''' + self.questions = self.get_questions() + + def welcome_message(self): + ''' + Display the welcome message + ''' + print("Welcome to AskPython Quiz!!") + + def start_game(self, start = None): + ''' + Question to start a game + Parameters: + start (string): This is the initialization answer by user + + bools: + True if the user answered yes, False otherwise the user answered no + ''' + while True: + if start == None: + start = input("Are you ready to play the Quiz? (yes/no) :").lower() + + if start == "yes": + return True + + elif start == "no": + print("See you next time!!") + return False + + else: + print("Invalid! Enter yes or no") + start = None + + def get_questions(self): + ''' + Return a list of questions + ''' + return [ + {"question": "Question 1: What is your Favourite programming language?", + "answer": "python"}, + {"question": "Question 2: Do you follow any author on AskPython?", + "answer": "yes"}, + {"question": "Question 3: What is the name of your favourite website for learning Python?", + "answer": "askpython"}, + ] + + def ask_question(self, question, correct_answer): + ''' + Ask a question and return the correct or incorrect + + parameters: + question (string): the answer user input to the question + correct_answer (string): the correct answer to the question + + returns: + return the point that user can get from the question + ''' + answer = input(question) + if answer == correct_answer: + print("correct\n") + return 1 + else: + print("Wrong Answer :(\n") + return 0 + + def show_result(self, score, total): + ''' + Show the result of the game + Parameters: + score (int):the score that user got from the game + total (int):total number of questions + + return: + mark(int): the percentage that the user got correct from the game + ''' + print(f"Thank you for Playing this small quiz game, you attempted {score}, questions correctly!\n") + mark = int((score / total) * 100) + print("<<<<>>>>") + self.show_answer() + print(f"Marks obtained: {mark}%\n") + return mark + + def show_answer(self): + ''' + Show all correct answers in the game + ''' + count = 1 + for question in self.questions: + answer = question["answer"] + print(f"Question {count}:{answer}") + count += 1 diff --git a/projects/Quiz Game/test/__init__.py b/projects/Quiz Game/test/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/projects/Quiz Game/test/test_quiz_game.py b/projects/Quiz Game/test/test_quiz_game.py new file mode 100644 index 00000000..0369c83a --- /dev/null +++ b/projects/Quiz Game/test/test_quiz_game.py @@ -0,0 +1,48 @@ +import unittest +from ..src.gamesystem import QuizGameSystem + +class QuizGameTest(unittest.TestCase): + def setUp(self): + ''' + Set up the test cases + ''' + self.system = QuizGameSystem() + + def test_get_questions_items(self): + ''' + Test the get_questions_items + ''' + questions = self.system.get_questions() + self.assertEqual(questions[0]['question'], "Question 1: What is your Favourite programming language?") + self.assertEqual(questions[0]['answer'], "python") + + def test_start_game_yes(self): + ''' + Test when the user answered yes it returns true + ''' + result = self.system.start_game(start="yes") + self.assertTrue(result) + + def test_start_game_no(self): + ''' + Test when the user answered no it returns false + ''' + result = self.system.start_game(start="no") + self.assertFalse(result) + + def test_show_result_all_correct(self): + ''' + Test when the answer is all correct, it returns 100% + ''' + mark = self.system.show_result(3,3) + self.assertEqual(mark, 100) + + def test_show_result_correct_one(self): + ''' + Test when one answer is correct, it returns 33% + ''' + mark = self.system.show_result(1,3) + self.assertEqual(mark, 33) + +if __name__ == "__main__": + unittest.main() \ No newline at end of file