Trying out project from pynative.com
Thank you GOA :)
Progress so far (1 - 6)
TODO.1. Define an Empty Vehicle Class
class Vehicle:
pass
print(type(Vehicle()))
TODO.2. Vehicle Class with Instance Attributes
class Vehicle:
def __init__(self, max_speed, mileage):
self.speed = max_speed
self.mileage = mileage
model1 = Vehicle(500, 1300)
print(model1.speed)
print(model1.mileage)
TODO.3. Rectangle Class with Area & Perimeter
class Rectangle:
def __init__(self, length, width):
self.length = length
self.width = width
def area(self):
return self.length * self.width
def perimeter(self):
return 2*(self.length + self.width)
rectangle1 = Rectangle(1,2)
print(rectangle1.area())
print(rectangle1.perimeter())
TODO.4. Student Class with Average Grade
class Student:
def __init__(self, name, marks):
self.name = name
self.marks = marks
def average(self):
return sum(self.marks) / len(self.marks)
s1 = Student("Alice", [85, 90, 78, 92, 88])
print(s1.average())
TODO.5. Product Class with Stock Value Calculator
class Product:
def __init__(self, name, price, quantity):
self.name = name
self.price = price
self.quantity = quantity
def total_value(self):
return self.price * self.quantity
p1 = Product("Laptop", 899.99, 5)
print(p1.total_value())
TODO.6. Bank Account with Deposit & Overdraw Protection
class BankAccount:
def __init__(self, balance):
self.balance = balance
def deposit(self, amount):
self.balance += amount
print(f"Balance after deposit: {self.balance}")
def withdraw(self, amount):
if self.balance >= amount:
self.balance -= amount
print(f"Balance after withdraw: {self.balance}")
else:
print(f"Insufficient funds. Current balance: {self.balance}")
ba1 = BankAccount(1000)
ba1.deposit(500)
ba1.withdraw(200)
ba1.withdraw(2000)
Comments
Post a Comment