Skip to content

Instantly share code, notes, and snippets.

@Midnex
Created June 12, 2021 18:47
Show Gist options
  • Save Midnex/c6c4f017bb9b33d2af6ef356e871226c to your computer and use it in GitHub Desktop.
Save Midnex/c6c4f017bb9b33d2af6ef356e871226c to your computer and use it in GitHub Desktop.
Build a Quiz Application in Python - The Nerdy Dev
# Video @ https://www.youtube.com/watch?v=kqa-BYI46ss
# Author: The Nerdy Dev
quiz_data = [
{
"question": "What color is the sky?",
"answer": "blue"
},
{
"question": "What color is the grass?",
"answer": "green"
},
{
"question": "What does Y E S spell?",
"answer": "yes"
},
{
"question": "What does E Y E S spell?",
"answer": "eyes"
},
{
"question": "That didn't work so well did it?",
"answer": "no"
},
{
"question": "Is this the end?",
"answer": "yes"
},
]
# Video @ https://www.youtube.com/watch?v=kqa-BYI46ss
# Author: The Nerdy Dev
from data import quiz_data
class Question:
def __init__(self, question_text, question_answer) -> None:
self.question_text = question_text
self.question_answer = question_answer
class Quiz:
def __init__(self, questions) -> None:
self.questions = questions
self.current_score = 0
self.question_number = 0
def ask_question(self, question):
return input(f"{question} ")
def has_more_questions(self):
return self.question_number < len(self.questions)
def check_for_correctness(self, user_answer, correct_answer):
if user_answer.strip().lower() == correct_answer.strip().lower():
self.current_score += 1
print("Correct")
else:
print(f"Incorrect, the correct answer was {correct_answer}")
print(f"Current Score: {self.current_score}")
def load_next_question(self):
current_question = self.questions[self.question_number]
self.question_number += 1
prepared_question = f"Q{self.question_number}: {current_question.question_text}"
user_answer = self.ask_question(prepared_question)
self.check_for_correctness(
user_answer, current_question.question_answer)
def main():
questions = []
for question in quiz_data:
question_text = question["question"]
question_answer = question["answer"]
prepared_question = Question(question_text, question_answer)
questions.append(prepared_question)
print("Questions are now loaded.")
quiz = Quiz(questions)
while quiz.has_more_questions():
quiz.load_next_question()
print("Thanks for playing!")
print(f"Your final score is {quiz.current_score}/{len(quiz.questions)}.")
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment