-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path101-square.py
More file actions
executable file
·73 lines (66 loc) · 2.54 KB
/
101-square.py
File metadata and controls
executable file
·73 lines (66 loc) · 2.54 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
#!/usr/bin/python3
"""a square class with methods in it"""
class Square:
"""init function"""
def __init__(self, size=0, position=(0, 0)):
"""initializes a private attribute"""
self.size = size
self.position = position
"""Retrieves the value from the hidden attribute"""
@property
def size(self):
return self.__size
""""sets the value retrieved to the size attribute"""
@size.setter
def size(self, value):
"""checks if data type is int else raises TypeError"""
if not isinstance(value, int):
raise TypeError("size must be an integer")
"""Raises ValueError if size is less than 0"""
if value < 0:
raise ValueError("size must be >= 0")
"""initializes a public attribute"""
self.__size = value
"""Retrieve the private attribute"""
@property
def position(self):
return self.__position
"""initializes position attribute to public"""
@position.setter
def position(self, value):
"""modify the attribute value"""
if not isinstance(value, tuple) or len(value) != 2:
raise TypeError("position must be a tuple of 2 positive integers")
if not isinstance(value[0], int) or not isinstance(value[1], int):
raise TypeError("position must be a tuple of 2 positive integers")
if value[0] < 0 or value[1] < 0:
raise TypeError("position must be a tuple of 2 positive integers")
self.__position = value
"""a public method that determines the area of a square"""
def area(self):
"""returns the area of square"""
return self.__size ** 2
"""prints # """
def my_print(self):
"""print the square to stdout with xcter ###"""
if self.__size == 0:
print()
else:
for i in range(self.__position[1]):
print()
for i in range(self.__size):
for x in range(self.__position[0]):
print(" ", end="")
for n in range(self.__size):
print("#", end="")
print()
def __str__(self):
"""Define the print() representation of a Square."""
if self.__size != 0:
[print("") for i in range(0, self.__position[1])]
for i in range(0, self.__size):
[print(" ", end="") for j in range(0, self.__position[0])]
[print("#", end="") for k in range(0, self.__size)]
if i != self.__size - 1:
print("")
return ("")