Skip to content

Commit c0baacf

Browse files
authored
Merge pull request #33 from code-for-tomorrow/kehao-chapter12
Add Ch. 12 exercises
2 parents 9c70b31 + 6ae4cc8 commit c0baacf

File tree

4 files changed

+78
-0
lines changed

4 files changed

+78
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Create a class called Student with instance attributes: name and age.
2+
# The user can input the name and age. Add 2 methods to the class:
3+
# 1. A raise_hand method which prints out the student's name followed
4+
# by "is now raising their hand."
5+
# 2. A grow_older method that makes the student older by 1 year.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
8+
# Student class implemented below. Teacher class uses it.
9+
10+
11+
class Student:
12+
def __init__(self, name, age):
13+
self.name = name
14+
self.age = age
15+
16+
def raise_hand(self):
17+
print(self.name + " is now raising their hand.")
18+
19+
def grow_older(self):
20+
self.age += 1
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Create a class called Student with instance attributes: name and age.
2+
# The user can input the name and age. Add 2 methods to the class:
3+
# 1. A raise_hand method which prints out the student's name followed
4+
# by "is now raising their hand."
5+
# 2. A grow_older method that makes the student older by 1 year.
6+
7+
8+
class Student:
9+
def __init__(self, name, age):
10+
self.name = name
11+
self.age = age
12+
13+
def raise_hand(self):
14+
print(self.name + " is now raising their hand.")
15+
16+
def grow_older(self):
17+
self.age += 1
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
8+
# Student class implemented below. Teacher class uses it.
9+
10+
11+
class Teacher:
12+
def __init__(self, name, age, students):
13+
self.name = name
14+
self.age = age
15+
self.students = students
16+
17+
def display_students(self):
18+
for student in self.students:
19+
print(student.name)
20+
21+
def graduate_students(self):
22+
for student in self.students:
23+
student.grow_older()
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

Comments
 (0)