Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modularization code and add unit test to Quiz Game #764

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added projects/Quiz Game/issue-734.diff
Binary file not shown.
Empty file.
18 changes: 18 additions & 0 deletions projects/Quiz Game/src/gameplay.py
Original file line number Diff line number Diff line change
@@ -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()
96 changes: 96 additions & 0 deletions projects/Quiz Game/src/gamesystem.py
Original file line number Diff line number Diff line change
@@ -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("<<<<<Correct answers>>>>>")
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
Empty file.
48 changes: 48 additions & 0 deletions projects/Quiz Game/test/test_quiz_game.py
Original file line number Diff line number Diff line change
@@ -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()