-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5.1.2.py
More file actions
81 lines (69 loc) · 2.5 KB
/
Copy path5.1.2.py
File metadata and controls
81 lines (69 loc) · 2.5 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#Below we have given you the code for three classes: Owner,
#Pet, and Name.
#
#An Owner is defined by two attributes: a Name and a list of
#Pets. The list of pets is initially empty; it can be added
#to later.
#
#A Pet is defined by two attributes: a Name and an Owner.
#
#A Name is defined by two attributes, both strings,
#representing first and last name.
#
#Write a function called get_owner_string that will take as
#input a single instance of Pet. The function should then print
#out the Pet's Owner's name using the following format:
#
#Boggle Joyner's owner is David Joyner.
#
#You will need to access the Pet's first name, pet's last name,
#pet's owner's first name, and pet's owner's last name to
#accomplish this. You may NOT modify the Name, Pet, or Owner
#classes (we will test your code with our own copies of these
#classes, so any changes you make will not be part of our
#grading code).
#
#HINT: To access a pet's name, you would use the_pet.name. So,
#to access only the pet's first name, you would use
#the_pet.name.first. To access a pet's owner's, you would use
#the_pet.owner. So, how would you access the pet's owner's
#first and last name?
class Name:
def __init__(self, first, last):
self.first = first
self.last = last
class Pet:
def __init__(self, name, owner):
self.name = name
self.owner = owner
class Owner:
def __init__(self, name):
self.name = name
self.pets = []
#Add your get_owner_string function here!
def get_owner_string(pet):
pet_first = pet.name.first
pet_last = pet.name.last
owner_first = pet.owner.name.first
owner_last = pet.owner.name.last
return(pet_first.__str__() + " " + pet_last.__str__() + "'s owner is " + owner_first.__str__() + " " + owner_last.__str__() + ".")
#Below are some lines of code that will test your function.
#You can change the value of the variable(s) to test your
#function with different inputs.
#
#If your function works correctly, this will originally
#print:
#Boggle Joyner's owner is David Joyner.
#Artemis Joyner's owner is David Joyner.
#Pippin Hepburn's owner is Audrey Hepburn.
owner_1 = Owner(Name("David", "Joyner"))
owner_2 = Owner(Name("Audrey", "Hepburn"))
pet_1 = Pet(Name("Boggle", "Joyner"), owner_1)
pet_2 = Pet(Name("Artemis", "Joyner"), owner_1)
pet_3 = Pet(Name("Pippin", "Hepburn"), owner_2)
owner_1.pets.append(pet_1)
owner_1.pets.append(pet_2)
owner_2.pets.append(pet_3)
print(get_owner_string(pet_1))
print(get_owner_string(pet_2))
print(get_owner_string(pet_3))