-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpython_objects_exercise.py
82 lines (57 loc) · 2.29 KB
/
python_objects_exercise.py
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# Use of the Student class
# definition of Person class
class Person:
def __init__(self, first, last, year):
self.first_name = first
self.last_name = last
self.birth_year = year
def full_name(self):
return self.first_name + " " + self.last_name
def age(self, current_year):
return current_year - self.birth_year
def __str__(self):
return "The person's name is " + self.full_name() + ". Their birth year is " + str(self.birth_year)
# definition of Student class
class Student:
# the person parameter must be a Person object
def __init__(self, person, pwd):
self.person = person
self.password = pwd
self.projects = []
# use the full_name method for Person
def get_name(self):
return self.person.full_name()
def check_password(self, pwd):
return self.password == pwd
def get_projects(self):
return self.projects
def add_project(self, project):
self.projects.append(project)
#################################################
# Student adds code where appropriate
# definition of function assign
def assign(students, name, pwd, project):
for student in students:
if student.get_name() == name and student.check_password(pwd):
if project is not student.projects:
return student.projects.append(project)
###################################################
# Testing code
# create some Student objects using Person object
joe = Student(Person("Joe", "Warren", 52), "TopSecret")
joe.add_project("Create practice exercises")
joe.add_project("Implement Minecraft")
scott = Student(Person("Scott", "Rixner", 29), "CodeSkulptor")
scott.add_project("Beat Joe at RiceRocks")
john = Student(Person("John", "Greiner", 47), "outdoors")
# create a list of students
profs = [joe, scott, john]
# test assign
print joe.get_projects()
assign(profs, "Joe Warren", "TopSecret", "Practice RiceRocks")
print joe.get_projects()
print john.get_projects()
assign(profs, "John Greiner", "OUTDOORS", "Work on reflexes")
print john.get_projects()
assign(profs, "John Greiner", "outdoors", "Work on reflexes")
print john.get_projects()