0% found this document useful (0 votes)
6 views

lab 11 output

Uploaded by

xivehoh190
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

lab 11 output

Uploaded by

xivehoh190
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

TASK 1

class student:
def __init__(self,name,age,grade):
self.name=name
self.__age=age
self.__grade=grade
def display_info(self):
print("Name:",self.name)
print("Age:",self.__age)
print("Grade:",self.__grade)
s1 = student("ALI",18,"B")
print(s1.name)
print(s1.__age)
print(s1.__grade)
------------------------------------------------------
ALI
Traceback (most recent call last):
File "D:\New folder\open file.py", line 12, in <module>
print(s1.__age)
^^^^^^^^
AttributeError: 'student' object has no attribute '__age'

Process finished with exit code 1

------------------------------------------------------
task 2:
class student:
def __init__(self,name,age,grade):
self.name=name
self.__age=age
self.__grade=grade
def get_age(self):
return self.__age
def get_grade(self):
return self.__grade
s1=student("Ali",18,"B")
print(s1.get_age())
print(s1.get_grade())
-----------------------------------------------------

18
B
--------------------------------------------------------
task 3
class student:
def __init__(self, name, age, grade):
self.name = name
self.__age = age
self.__grade = grade

def get_age(self):
return self.__age

def set_age(self, new_age):


if 5 <= new_age <= 25:
self.__age = new_age
print(f"Age updated to {self.__age}")
else:
print("Invalid age. Must be between 5 and 25.")
def get_grade(self):
return self.__grade

def set_grade(self, new_grade):


if new_grade in ['a', 'b', 'c', 'd', 'f']:
self.__grade = new_grade
print(f"Grade updated to {self.__grade}")
else:
print("Invalid grade. Must be 'a', 'b', 'c', 'd', 'f' ")

def display_info(self):
print(f'Name: {self.name}, Age: {self.get_age()}, Grade:
{self.get_grade()}')

s1 = student('Ali', 18, 'B') # Invalid initial age, should be between 5 and 25


print(s1.name)
print(s1.get_grade())
print(s1.get_age())
s1.set_age(20)
s1.set_grade('a')
s1.set_age(30)
s1.set_grade('z')
s1.display_info()

------------------------------------------------------
Getter methods are used to access the private attributes. __age and __grade
Setter methods are used to modify the private attributes of a class.set_age() and
set_grade() are used to update the values of the private attributes __age and
__grade
Ali
B
18
Age updated to 20
Grade updated to a
Invalid age. Must be between 5 and 25.
Invalid grade. Must be 'a', 'b', 'c', 'd', 'f'
Name: Ali, Age: 20, Grade: a
-----------------------------------------------
task 4
Alice
Deposited 500. New balance: 1500
Withdrawn 200. New balance: 1300
----------------------------------------------
lab Assign
class BankAccount:
def __init__(self, name, balance, account_type):
self.name = name
self.__balance = balance
self.__account_type = account_type

def get_balance(self):
return self.__balance

def set_balance(self, balance):


if balance < 0:
print("Balance cannot be negative.")
else:
self.__balance = balance

def get_account_type(self):
return self.__account_type

def set_account_type(self, account_type):


valid_account_types = ['Savings', 'Current', 'Fixed Deposit']
if account_type in valid_account_types:
self.__account_type = account_type
else:
print("Invalid account type. Please choose from: Savings, Current,
Fixed Deposit.")

# Example usage
account = BankAccount("John Doe", 1000, "Savings")
print(account.name) # Public attribute, accessible directly

print(account.get_balance()) # Using getter to access private attribute


account.set_balance(2000) # Using setter to update private attribute
print(account.get_balance())

print(account.get_account_type()) # Using getter to access private attribute


account.set_account_type("Current") # Using setter to update private attribute
print(account.get_account_type())

You might also like