forked from fenyx-it-academy/Class7-Python-Module-Week4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1.py
More file actions
59 lines (44 loc) · 1.77 KB
/
1.py
File metadata and controls
59 lines (44 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
## 1. School
# 1. Create a `School` class with instance attribute `capacity`.
# 2. Add `students` as the class attribute. This will be a list and keep track of the students in the school.
# 3. Create a `Student` class with attributes: `name`, `age`, `gender`
# 4. Add `__str__` method to this class to print the objects.
# 6. Add `add_student` method to the class. If capacity is full print error message else add the student.
# 7. Add `print_students` method to print the all existing students. Loop through the students list and print each student object.
# 8. Create a `School` object and threee students, add first 2 students to school. Print students and afterwards try to add the third student.
# 9. Use `__dict__` method to see attributes
class School:
def __init__(self,capacity):
self.students=[]
self.capacity=capacity
def add_student (self,*student) :
if len(self.students) >= self.capacity :
print ('Capacity is full!')
else:
for s in student : #ability to add multiple students at once
self.students.append (s)
def print_students (self):
for s in self.students :
print (s.__str__())
class Student:
def __init__(self , name , age , gender):
self.name=name
self.age=age
self.gender=gender
def __str__(self):
return f'''
{self.name }
{self.age }
{self.gender}
'''
student1=Student('Henry s',19,'m')
student2=Student('J s',18,'f')
student3=Student('Mary x',17,'f')
school1=School (2)
#adding first two students :
school1.add_student(student1,student2)
school1.print_students()
#adding last student, but capacity is full!
school1.add_student(student3)
print (student1.__dict__)
print (school1.__dict__)