Still working on projects from pynative.com
Progress so far (8 - 12)
TODO.8. User Class with Password Validation
class User:
def __init__(self, username, password):
self.username = username
self.password = password
def check_password(self, input_password):
if input_password == self.password:
return True
else:
return False
user = User("Alice", "secure123")
print(user.check_password("secure123"))
print(user.check_password("password"))
TODO.9. Temperature Class with Unit Converters
class Temperature:
def __init__(self, temp):
self.temp = temp
print(f"Celsius: {self.temp}")
def to_fahrenheit(self, temp):
print(f"Fahrenheit: {(self.temp * 1.8) + 32}")
def to_kelvin(self, temp):
print(f"Kelvin: {(self.temp + 273.15)}")
temperature = Temperature(100)
temperature.to_fahrenheit(100)
temperature.to_kelvin(100)
TODO.10. Notebook Class with Add & Display Notes
class Notebook:
def __init__(self, notes):
self.notes = notes
def add_note(self, note):
self.notes.append(note)
def show_notes(self):
notes = self.notes
for i, item in enumerate(notes):
print(f"{i+1}. {item}")
notebook = Notebook([])
notebook.add_note("Buy groceries")
notebook.add_note("Read a Book")
notebook.add_note("Call the doctor")
notebook.show_notes()
TODO.11 Coffee Machine with Multi-Resource Tracking
class CoffeeMachine:
def __init__(self, water, coffee, milk):
self.water = water
self.coffee = coffee
self.milk = milk
def make_latte(self):
required_water = 200
required_coffee = 20
required_milk = 150
remaining_water = self.water - required_water
remaining_coffee = self.coffee - required_coffee
remaining_milk = self.milk - required_milk
if remaining_water >= 0 and remaining_coffee >= 0 and remaining_milk >= 0:
print(f"Latte made! Remaining - Water: {remaining_water}ml, Coffee: {remaining_coffee}g, "
f"Milk: {remaining_milk}ml")
else:
print("Not enough resources to make a latte")
coffee_machine1 = CoffeeMachine(300, 100, 200)
coffee_machine1.make_latte()
coffee_machine2 = CoffeeMachine(200, 10, 100)
coffee_machine2.make_latte()
TODO.12 Shared Class Attribute Across Instances
class Vehicle:
def __init__(self):
self.color = "White"
car = Vehicle()
print(car.color)
bus = Vehicle()
print(bus.color)
Comments
Post a Comment