Day 17-OOP & Quiz Game


Posted by pei_______ on 2022-04-27

learning from 100 Days of Code: The Complete Python Pro Bootcamp for 2022


物件導向程式設計 Object Oriented Programming

(1) 類別 (Class)
(2) 物件 (Object)
(3) 屬性 (Attribute)
(4) 建構式 (Constructor)
(5) 方法 (Method)

常見命名規則與應用

(1) PascalCase => PythonClass
(2) camelCase => javaMethod()
(3) snake_class => python_variable


# Question_model.py

# 問題 Class: 建立"單一問題"的模組
class Qustion: 

    # 建構式 Constructor: "Obect = 每一個問題" 的固定架構
    def __init__(self, q_text, q_answer):  
        self.text = q_text  # 屬性 Attribute
        self.answer = q_answer  # 屬性 Attribute
# quiz_brain.py

# 問題集 Class: 建立"問題集"的模組
class QuizBrain: 


    # 建構式 Constructor: "Obect = 每一個問題集" 的固定架構
    def __init__(self, question_list):
        self.question_number = 0 # 屬性 Attribute => 用來呼叫 list
        self.question_list = question_list  # 屬性 Attribute
        self.score = 0  # 屬性 Attribute => 預設分數,起始為 0


    # 方法 Method : "每一個問題集"  => 確認是否還有剩餘的問題
    def still_has_question(self): 
        return self.question_number < len(self.question_list)
        # 若問題集的屬性(number) < 問題集的屬性(list的長度)
        # 回傳 True, 否則回傳 False


    # 方法 Method : "每一個問題集"  => 可以接收 (input) 一個答案
    def next_question(self): 
        current_question = self.question_list[self.question_number]
        # 等同 current_question = question_list[0]        
        self.question_number += 1 
        # 問完之後問題自動+1
        user_answer = input(f"Q.{self.question_number}: {current_question.text} (True/False): ")
        # 讓 user輸入答案,並存取答案
        self.check_answer(user_answer, current_question.answer)
        # 以 user答案 及 正確答案進行比對


    # 方法 Method : "每一個問題集"  => 將接收到的答案和正確答案比對
    def check_answer(self, user_answer, correct_answer):

        if user_answer.lower() == correct_answer.lower():
            print("You got it right!")
            self.score += 1
        else:
            print("That's wrong.")

        print(f"The correct answer was: {correct_answer}.")
        print(f"You current score is:{self.score}/{self.question_number}")
        print("\n")
# main.py

from question_model import Qustion
from data import question_data
from quiz_brain import QuizBrain

# List 中每筆資料都是 Object 的位址
question_bank = []  

# 在每一個 question_data list裡的資料 (dictionary)
for question in question_data: 

    question_text = question['question']
    # 存入 dictionary 裡的 text
    question_answer = question['correct_answer']  
    # 存入 dictionary 裡的 answer
    new_question = Qustion (question_text, question_answer)  
    # 都當成創建物件的原料,創建 問題 Object
    question_bank.append(new_question)  
    # 再把 Object(位址) 輪流放進 List 裡


# 以 List 作為材料,創建 問題集 Object
quiz = QuizBrain(question_bank)

# while迴圈: 確認 "每一個問題集" 是否有剩餘的問題
while quiz.still_has_question():
    # 印出問題並計分
    quiz.next_question()

# while迴圈結束: 結束遊戲
print("You've completed the quiz")
print(f"Your final score was: {quiz.score}/{len(question_bank)}")
# data.py

# 存取問題和答案的 list
question_data = [
{"question": " question_text 1.", 
"correct_answer": "True"}
{"question": " question_text 2.", 
"correct_answer": "False"}
]

#Python #課堂筆記 #100 Days of Code







Related Posts

〈 演算法與資料結構 #1〉Recursion 遞迴函式

〈 演算法與資料結構 #1〉Recursion 遞迴函式

布林代數(Boolean Algebra)

布林代數(Boolean Algebra)

後端好朋友 Express middleware

後端好朋友 Express middleware


Comments