A template for objects · in code: Classes and Objects
A template for objects: a name, and the fields every object made from it has. Pet("Tom") makes a new Pet; the values in the brackets fill its fields in order, and the rest start at the class's own values.
Pet. Class names start with a capital letter, by habit.energy = 5. Starting values are plain values: numbers, text, lists or true/false.Open it in the editor, press Run, then change it and run it again.
class Account: def __init__(self, owner="", balance=0): self.owner = owner self.balance = balance
def deposit(self, amount): self.balance = self.balance + amount
def withdraw(self, amount): if amount <= self.balance: self.balance = self.balance - amount else: print("Sorry " + self.owner + ", there's only ₹" + str(self.balance) + " in your account.")
asha = Account("Asha", 100)ravi = Account("Ravi")asha.deposit(50)ravi.deposit(20)asha.withdraw(500)ravi.withdraw(5)print(asha.owner + ": ₹" + str(asha.balance))print(ravi.owner + ": ₹" + str(ravi.balance))pets = [Pet("Tom"), Pet("Bella", 8)].pet.energy. Change it with a Change a Field block.this is the object it runs on, so this.energy is that pet's own energy.pet, and the method's name, like feed.