|
1 | | -# Create a class called Teacher. Add 3 variables to this |
2 | | -# class: Name, Age, and an array of Student classes from student_class. |
3 | | -# The user can input these 3 variables. Add 2 Methods to the class: A |
4 | | -# displayClass method that prints out the names of all the students. |
5 | | -# A graduate method that increments the age of all of his/her students by 1. |
6 | | -# Then prints out all the ages. |
| 1 | +# Create a class called Teacher. Add 3 instance variables to this |
| 2 | +# class: name, age, and students (a list of Student objects). |
| 3 | +# Add 2 Methods to the class: A display_students method that |
| 4 | +# prints out the names of all the students, each on their own line, and |
| 5 | +# a graduate_students method that increments the age of all of the |
| 6 | +# teacher's Students by 1. Then it should print out all their ages. |
7 | 7 |
|
8 | 8 | # Student class implemented below. Teacher class uses it. |
9 | 9 |
|
10 | 10 |
|
11 | | -class Student: |
12 | | - def __init__(self, name, age): |
13 | | - self.name = name |
14 | | - self.age = age |
15 | | - |
16 | | - def raiseHand(self): |
17 | | - print(self.name + " is now raising their hand.") |
18 | | - |
19 | | - def growOlder(self): |
20 | | - self.age += 1 |
21 | | - |
22 | | - |
23 | 11 | class Teacher: |
24 | 12 | def __init__(self, name, age, students): |
25 | 13 | self.name = name |
26 | 14 | self.age = age |
27 | 15 | self.students = students |
28 | 16 |
|
29 | | - def displayClass(self): |
| 17 | + def display_students(self): |
30 | 18 | for student in self.students: |
31 | 19 | print(student.name) |
32 | 20 |
|
33 | | - def graduate(self): |
| 21 | + def graduate_students(self): |
34 | 22 | for student in self.students: |
35 | | - student.age += 1 |
| 23 | + student.grow_older() |
36 | 24 | print(student.age) |
| 25 | + |
| 26 | + |
| 27 | +class Student: |
| 28 | + def __init__(self, name, age): |
| 29 | + self.name = name |
| 30 | + self.age = age |
| 31 | + |
| 32 | + def raise_hand(self): |
| 33 | + print(self.name + " is now raising their hand.") |
| 34 | + |
| 35 | + def grow_older(self): |
| 36 | + self.age += 1 |
0 commit comments