Classes
Classes are the basic building blocks of a structured program. It provides for cleaner code. And for storing related data.
What is related data? A person's first name and last name is related.
classes.py
class Pets: def __init__(self): self.list_of_pets = [] def get_menu_option(self): count = len(self.list_of_pets) option = input(f'{count} A)dd Pet R)emove Pet L)ist pets Q)uit:') return option def add_pet(self): pet_name = input('Enter pet name to add:') self.list_of_pets.append(pet_name) def remove_pet(self): pet_name = input('Enter pet name to remove:') self.list_of_pets.remove(pet_name) def list_pets(self): for pet in self.list_of_pets: print(pet) def run(self): while True: option = self.get_menu_option() match option: case 'a': self.add_pet() case 'r': self.remove_pet() case 'l': self.list_pets() case 'q': # exit the while loop break case default: print('Invalid option, try again') pets = Pets() pets.run() print("Done.")
Output:
0 A)dd Pet R)emove Pet L)ist pets Q)uit:a Enter pet name to add:tootsie 1 A)dd Pet R)emove Pet L)ist pets Q)uit:a Enter pet name to add:fido 2 A)dd Pet R)emove Pet L)ist pets Q)uit:l tootsie fido 2 A)dd Pet R)emove Pet L)ist pets Q)uit:r Enter pet name to remove:tootsie 1 A)dd Pet R)emove Pet L)ist pets Q)uit:l fido 1 A)dd Pet R)emove Pet L)ist pets Q)uit:q Done.
Another Class
classes.py
class Television: def __init__(self): self.channel = 0 self.volume = 0 self.isOn = False def isOn(self): return self.isOn def turnOn(self): self.isOn = True def turnOff(self): self.isOn = False def currentVolume(self): return self.volume def increaseVolume(self): if self.volume < 100: self.volume = self.volume + 1 def decreaseVolume(self): if self.volume > 0: self.volume = self.volume - 1 def currentChannel(self): return self.channel def changeChannel(self,channel): if channel > 0 and channel < 50: self.channel = channel tv = Television() if tv.isOn() == False: tv.turnOn() tv.changeChannel(3) tv.increaseVolume() tv.increaseVolume() tv.increaseVolume() tv.increaseVolume() tv.turnOff() print("Done.")
Links
Exercises (5 mins)
-
Complete the contact.py to match the output below.
contact.py
class Contact: def __init__(self): # initialize firstName and lastName to "" pass def getFullName(self): # return firstName with a space and lastName contact = Contact() contact.firstName = "Dave" contact.lastName = "Johnson" print(f'Your name is {contact.getFullName()}')
Output:
Your name is Dave Johnson
-
Modify functions.py and create a money class.
functions.py
def get_menu_option(): option = input(f'${balance} A)dd Money S)ubtract Money Q)uit:') return option def get_money(): money = input('Enter amount:') return int(money) def add_money(): # you need access to the global balance global balance print('Add money') money = get_money() balance = balance + money def subtract_money(): # you need access to the global balance global balance print('Subtract money') money = get_money() balance = balance - money def invalid_option(): print('Invalid option, try again') balance = 0 # do the while loop forever while True: option = get_menu_option() match option: case 'a': add_money() case 's': subtract_money() case 'q': # exit the while loop break case default: invalid_option() print("Done.")