Day 017

AWESOMee·2022년 2월 9일
0

Udemy Python Bootcamp

목록 보기
17/64
post-thumbnail

Udemy Python Bootcamp Day 017

the name of the class should have the first letter of every word capitalized.

PascalCase
camelCase
snake_case

Pascal case being used for the class names,
snake case being used pretty much to name everything else.

Constructor

a part of blueprint that allows us to specify what should happen when our object is being constructed.

In python, the way that we would create the constructor is by using a special function,

class Car:
	def __init__(self):
    #initialise attributes

two underscores either side of the name means that it's a method that the Python interpreter knows about and knows that it has a special function.

Special funcion is normally used to initialize the attributes.

The attributes are the things that the object will have.
And they are basically just variables that are associated with the final object.

class User:

    def __init__(self):
        print("new user being created...")


user_1 = User()
user_1.id = "001"
user_1.username = "awesomee"

print(user_1.username)

user_1 = User()
user_1.id = "002"
user_1.username = "jack"


# output
new user being created...
awesomee
new user being created...

Attribute

class Car:
	def __init__(self, seats):
    	self.seats = seats

class User:

    def __init__(self, user_id, username):
        self.id = user_id
        self.username = username


user_1 = User("001", "awesomee")

print(user_1.username)  # equal to print(user_1.id)

# output
awesomee

class User:

    def __init__(self, user_id, username):
        self.id = user_id
        self.username = username
        self.followers = 0  # default value of zero


user_1 = User("001", "awesomee")
user_2 = User("002", "jack")

print(user_1.followers)


# output
0

Adding to Methods to a Class

attributes : seats = 5
methods: def enter_race_mode():
		      seats = 2

class User:

    def __init__(self, user_id, username):
        self.id = user_id
        self.username = username
        self.followers = 0
        self.following = 0

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


user_1 = User("001", "awesomee")
user_2 = User("002", "jack")

user_1.follow(user_2)
print(user_1.followers)
print(user_1.following)
print(user_2.followers)
print(user_2.following)


# output
0
1
1
0

Quiz Project

Part 1

class Question:

    def __init__(self, q_text, q_answer):
        self.text = q_text
        self.answer = q_answer


new_q = Question("hello", "False")
print(new_q.text)


# output
hello

Part 2

from question_model import Question
from data import question_data

question_bank = []
for question in question_data:
    question_text = question["text"]
    question_answer = question["answer"]
    new_question = Question(question_text, question_answer)
    question_bank.append(new_question)

print(question_bank)

Part 3

class QuizBrain:

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

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

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["text"]
    question_answer = question["answer"]
    new_question = Question(question_text, question_answer)
    question_bank.append(new_question)

quiz = QuizBrain(question_bank)
quiz.next_question()


# output
Q.1: A slug's blood is green. (True/False): 

Part 4

class QuizBrain:

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

    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
        input(f"Q.{self.question_number}: {current_question.text} (True/False): ")

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["text"]
    question_answer = question["answer"]
    new_question = Question(question_text, question_answer)
    question_bank.append(new_question)

quiz = QuizBrain(question_bank)

while quiz.still_has_questions():  # if quiz still has questions remaining
    quiz.next_question()

self.question_number < len(self.question_list) this value is then going to be straight away returned by this method back into this while loop to see if we should continue going to the next question or not.

Part 5

class QuizBrain:

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

    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"Q.{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.score += 1
            print("You got it")
        else:
            print("That's wrong.")
        print(f"The correct answer was: {correct_answer}.")
        print(f"Your current score is: {self.score}/{self.question_number}")
        print("\n")

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["text"]
    question_answer = question["answer"]
    new_question = Question(question_text, question_answer)
    question_bank.append(new_question)

quiz = QuizBrain(question_bank)

while quiz.still_has_questions():
    quiz.next_question()

print("You've completed the quiz")
print(f"Your final score was: {quiz.score}/{quiz.question_number}")

profile
개발을 배우는 듯 하면서도

0개의 댓글