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

feat: added python quiz game using oop #342

Open
wants to merge 1 commit 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
102 changes: 102 additions & 0 deletions GAMES/Quiz-Game-OOP/data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
question_data = [
{
"category": "Science: Computers",
"type": "boolean",
"difficulty": "medium",
"question": "The HTML5 standard was published in 2014.",
"correct_answer": "True",
"incorrect_answers": [
"False"
]
},
{
"category": "Science: Computers",
"type": "boolean",
"difficulty": "medium",
"question": "The first computer bug was formed by faulty wires.",
"correct_answer": "False",
"incorrect_answers": [
"True"
]
},
{
"category": "Science: Computers",
"type": "boolean",
"difficulty": "medium",
"question": "FLAC stands for 'Free Lossless Audio Condenser'.",
"correct_answer": "False",
"incorrect_answers": [
"True"
]
},
{
"category": "Science: Computers",
"type": "boolean",
"difficulty": "medium",
"question": "All program codes have to be compiled into an executable file in order to be run. This file can then be executed on any machine.",
"correct_answer": "False",
"incorrect_answers": [
"True"
]
},
{
"category": "Science: Computers",
"type": "boolean",
"difficulty": "easy",
"question": "Linus Torvalds created Linux and Git.",
"correct_answer": "True",
"incorrect_answers": [
"False"
]
},
{
"category": "Science: Computers",
"type": "boolean",
"difficulty": "easy",
"question": "The programming language 'Python' is based off a modified version of 'JavaScript'",
"correct_answer": "False",
"incorrect_answers": [
"True"
]
},
{
"category": "Science: Computers",
"type": "boolean",
"difficulty": "medium",
"question": "AMD created the first consumer 64-bit processor.",
"correct_answer": "True",
"incorrect_answers": [
"False"
]
},
{
"category": "Science: Computers",
"type": "boolean",
"difficulty": "easy",
"question": "'HTML' stands for Hypertext Markup Language.",
"correct_answer": "True",
"incorrect_answers": [
"False"
]
},
{
"category": "Science: Computers",
"type": "boolean",
"difficulty": "easy",
"question": "In most programming languages, the operator ++ is equivalent to the statement '+= 1'.",
"correct_answer": "True",
"incorrect_answers": [
"False"
]
},
{
"category": "Science: Computers",
"type": "boolean",
"difficulty": "hard",
"question": "The IBM PC used an Intel 8008 microprocessor clocked at 4.77 MHz and 8 kilobytes of memory.",
"correct_answer": "False",
"incorrect_answers": [
"True"
]
}
]
16 changes: 16 additions & 0 deletions GAMES/Quiz-Game-OOP/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from data import question_data
from question_model import Question
from quiz_brain import QuizBrain

quiz_list = []
for question in question_data:
q_object = Question(question["question"], question["correct_answer"])
quiz_list.append(q_object)

quiz = QuizBrain(quiz_list)

while quiz.still_has_question():
quiz.next_question()

print("You've completed the quiz")
print(f"Your final score was: {quiz.score}/{quiz.question_num}")
4 changes: 4 additions & 0 deletions GAMES/Quiz-Game-OOP/question_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Question:
def __init__(self, text, answer):
self.text = text
self.answer = answer
24 changes: 24 additions & 0 deletions GAMES/Quiz-Game-OOP/quiz_brain.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class QuizBrain:
def __init__(self, question_list):
self.question_list = question_list
self.score = 0
self.question_num = 0

def still_has_question(self):
return self.question_num < len(self.question_list)

def next_question(self):
current_question = self.question_list[self.question_num]
self.question_num += 1
user_answer = input(f"Q.{self.question_num}: {current_question.text} (True/False): ")
self.check_answer(user_answer, current_question.answer)

def check_answer(self, user_answer, correct_answer):
if user_answer.lower() == correct_answer.lower():
self.score += 1
print("You got it right!")
else:
print("That's wrong!")
print(f"The correct answer was: {correct_answer}.")
print(f"Your current score is: {self.score}/{self.question_num}")
print("\n")
Loading