-
Notifications
You must be signed in to change notification settings - Fork 23
/
10_solutions.py
123 lines (97 loc) · 3.48 KB
/
10_solutions.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# -*- coding: utf-8 -*-
"""10_solutions.py
Author -- Michael Widrich
Contact -- [email protected]
Date -- 01.10.2019
###############################################################################
The following copyright statement applies to all code within this file.
Copyright statement:
This material, no matter whether in printed or electronic form, may be used for
personal and non-commercial educational use only. Any reproduction of this
manuscript, no matter whether as a whole or in parts, no matter whether in
printed or in electronic form, requires explicit prior acceptance of the
authors.
###############################################################################
Example solutions for tasks in file 10_tasks.py.
"""
###############################################################################
# 10 classes
###############################################################################
#
# Task 1
#
# Create a class 'Face' with an attribute 'orientation' and two methods
# 'look(new_orientation)' and 'show()'.
# look() shall take a string new_orientation as argument. If new_orientation is
# the string "left", let the attribute 'orientation' point to string "left". If
# new_orientation is the string "right", let the attribute 'orientation' point
# to string "right".
# 'show()' should print a face in 'right' or 'left' orientation, based
# on the current value of 'orientation'.
# Example usage:
# face = Face()
# face.look('left')
# face.show() # prints output '<.<'
# face.look('right')
# face.show() # prints output '>.>'
# Your code here #
class Face:
def __init__(self):
self.orientation = 'left'
def look(self, new_orientation):
if new_orientation == "left":
self.orientation = new_orientation
elif new_orientation == "right":
self.orientation = new_orientation
def show(self):
if self.orientation == 'left':
print('<.<')
elif self.orientation == 'right':
print('>.>')
face = Face()
face.look('right')
face.show()
face.look('left')
face.show()
#
# Task 2
#
# Create a class 'OwlFace' that is derived from class Face from task 1.
# look() shall take a string new_orientation as argument. If new_orientation is
# the string "left", let the attribute 'orientation' point to string "left". If
# new_orientation is the string "right", let the attribute 'orientation' point
# to string "right". If new_orientation is the string "ahead", let the
# attribute 'orientation' point to string "ahead".
# 'show()' should print a face in 'right', 'left', or ahead orientation, based
# on the current value of 'orientation'.
# Example usage:
# face = Face()
# face.look('left')
# face.show() # prints output '(O<O)'
# face.look('right')
# face.show() # prints output '(O>O)'
# face.look('ahead')
# face.show() # prints output '(OvO)'
# Your code here #
class OwlFace(Face):
def look(self, new_orientation):
if new_orientation == "left":
self.orientation = new_orientation
elif new_orientation == "right":
self.orientation = new_orientation
elif new_orientation == "ahead":
self.orientation = new_orientation
def show(self):
if self.orientation == 'left':
print('(O<O)')
elif self.orientation == 'right':
print('(O>O)')
elif self.orientation == 'ahead':
print('(OvO)')
face = OwlFace()
face.look('right')
face.show()
face.look('left')
face.show()
face.look('ahead')
face.show()