100일의 코드: 2022년을 위한 완벽한 Python Pro 부트캠프 - 17일차(퀴즈 프로젝트 및 OOP의 이점)
data.py
question_data = [
{"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": "Pointers were not used in the original C programming language; they were added later on in C++.",
"correct_answer": "False",
"incorrect_answers": ["True"]
},
{"category": "Science: Computers",
"type": "boolean",
"difficulty": "easy",
"question": "RAM stands for Random Access Memory.",
"correct_answer": "True",
"incorrect_answers": ["False"]
},
{"category": "Science: Computers",
"type": "boolean",
"difficulty": "easy",
"question": "Ada Lovelace is often considered the first computer programmer.",
"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": "easy",
"question": "Time on Computers is measured via the EPOX System.",
"correct_answer": "False",
"incorrect_answers": ["True"]
},
{"category": "Science: Computers",
"type": "boolean",
"difficulty": "easy",
"question": "The NVidia GTX 1080 gets its name because it can only render at a 1920x1080 screen resolution.",
"correct_answer": "False",
"incorrect_answers": ["True"]
},
{"category": "Science: Computers",
"type": "boolean",
"difficulty": "easy",
"question": "Linux was first created as an alternative to Windows XP.",
"correct_answer": "False",
"incorrect_answers": ["True"]
},
{
"category": "Science: Computers",
"type": "boolean",
"difficulty": "easy",
"question": "The Python programming language gets its name from the British comedy group Monty Python",
"correct_answer": "True", "incorrect_answers": ["False"]
},
]
question_model.py(클래스 질문 생성, 클래스 초기화, 메소드 및 속성 생성).
class Question:
def __init__(self, q_text, q_answer):
self.text = q_text
self.answer = q_answer
Quiz_brain.py
class QuizBrain:
def __init__(self, q_list):
self.question_list = q_list
self.question_number = 0
self.question_score = 0
def still_has_questions(self):
return self.question_number < len(self.question_list)
def next_question(self):
current_question = self.question_list[self.question_number]
self.question_number += 1
user_answer = input(f"{self.question_number}: {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.question_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.question_score}/{self.question_number}. ")
print("\n")
main.py
from question_model import Question
from data import question_data
from quiz_brain import QuizBrain
question_bank = []
for question in question_data:
question_text = question["question"]
question_answer = question["correct_answer"]
new_question = Question(q_text=question_text, q_answer=question_answer)
question_bank.append(new_question)
quiz = QuizBrain(question_bank)
while quiz.still_has_questions():
quiz.next_question()
print(f"Congratulations, you've completed the quiz. Your final score is {quiz.question_score}/{quiz.question_number}. ")
Reference
이 문제에 관하여(100일의 코드: 2022년을 위한 완벽한 Python Pro 부트캠프 - 17일차(퀴즈 프로젝트 및 OOP의 이점)), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/mike_kameta_aed62d48c2d0f/100-days-of-code-the-complete-python-pro-bootcamp-for-2022-day-17-the-quiz-project-and-benefits-of-oop-kce텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)