DEV Community

Palak Hirave
Palak Hirave

Posted on

Day 17 of 100

Hi! Today, I learned to make my own classes. I did run into a few problems whilst working on today's project but luckly I managed to figure it out.

Resource for generating quizzes - https://opentdb.com/

Notes

# Creating Class
class User:
    # Creating Attribute
    def __init__(self, user_id, username):
        self.user_id = user_id
        self.username = username
        self.followers = 0
        self.following = 0

    # Creating Method
    def follow_count(self):
        self.followers += 1
        print(f"Yay! You have {self.followers} followers.")

    def follow(self, user):
        self.following +=  1
        user.followers += 1

        print(f"Yay! You are following {self.following} user(s).")
        print(f"Yay! They now have {user.followers} follower(s).")

user_1 = User("001", "Palak")
print(user_1)
user_2 = User("002", "Hirave")
print(user_2)
print(user_1, user_2)

user_3 = User("003", "Damn")
# Adding another attribute
user_3.notes = "~Yay~"
print(user_3.notes)

# Having a default value (followers are 0) to use later
user_4 = User("004", "Kevin")
print(user_4.followers)
user_4.followers += 1
print(user_4.followers)
print(user_4.follow_count())

print(user_1.follow_count())
print(user_3.follow_count())

user_4.follow(user_1)
user_4.follow(user_1)
user_4.follow(user_1)
user_4.follow(user_1)

'''PascalCase - HelloHowAreYou
snake_case - hello_how_are_you
camelCase - helloHowAreYou'''
Enter fullscreen mode Exit fullscreen mode

Project

main.py

from data import question_data
from question_model import Question
from quiz_brain import QuizBrain

question_bank = []
for query in question_data:
    question = Question(query["question"], query["answer"])
    question_bank.append(question)

print(question_bank)

quiz_brain = QuizBrain(question_data)

game_on = True

print("Welcome to the quiz! Answer each question with a True or False. Good Luck!")
while game_on:
    quiz_brain.next_question()
    quiz_brain.check_answer()

    if False == quiz_brain.end_quiz():
        game_on = False

    if quiz_brain.question_number >= 12:
        print("Opps! We do not have any more questions! Good Job!")
        game_on = False

# TODO: ask the question
# TODO: checking if the answer is correct
# TODO: checking if we are at the end of the quiz
# TODO: Continue to show the next problem

Enter fullscreen mode Exit fullscreen mode

quiz_brain.py

from data import question_data

class QuizBrain:

    def __init__(self, q_list):
        self.question_number = 0
        self.questions_list = q_list

    def next_question(self):
        # TODO: ask the question
        global main_q, key
        key = (question_data[self.question_number])
        main_q = key["question"]
        self.question_number += 1

    def check_answer(self):
        # TODO: checking if the answer is correct
        global index
        user_input = str(input(f"{main_q} - True or False: "))
        correct_ans = key["answer"]
        index = 101
        if user_input == correct_ans:
            print("Correct!")
            index = 1

        else:
            print("Wrong!")
            index = 0

    def end_quiz(self):
        # TODO: checking if we are at the end of the quiz
        global game_on
        if index == 0:
            print(f"Game ending. Final score: {self.question_number - 1}")
            game_on = False
            return game_on

        else:
            if index == 1:
                game_on = True
                print(f"Your current score is: {self.question_number}")
                print("Moving on to the next question.")
                return game_on
Enter fullscreen mode Exit fullscreen mode

question_model.py

class Question:
    def __init__(self, question, answer):
        self.question = question
        self.answer = answer

Enter fullscreen mode Exit fullscreen mode

data.py

question_data = [
    {"question": "A slug's blood is green.", "answer": "True"},
    {"question": "The loudest animal is the African Elephant.", "answer": "False"},
    {"question": "Approximately one quarter of human bones are in the feet.", "answer": "True"},
    {"question": "The total surface area of a human lungs is the size of a football pitch.", "answer": "True"},
    {"question": "In West Virginia, USA, if you accidentally hit an animal with your car, you are free to take it home to eat.", "answer": "True"},
    {"question": "In London, UK, if you happen to die in the House of Parliament, you are entitled to a state funeral.", "answer": "False"},
    {"question": "It is illegal to pee in the Ocean in Portugal.", "answer": "True"},
    {"question": "You can lead a cow down stairs but not up stairs.", "answer": "False"},
    {"question": "Google was originally called 'Backrub'.", "answer": "True"},
    {"question": "Buzz Aldrin's mother's maiden name was 'Moon'.", "answer": "True"},
    {"question": "No piece of square dry paper can be folded in half more than 7 times.", "answer": "False"},
    {"question": "A few ounces of chocolate can to kill a small dog.", "answer": "True"}
]

Enter fullscreen mode Exit fullscreen mode

Top comments (0)