-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path101-square.py
70 lines (58 loc) · 2.14 KB
/
101-square.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
#!/usr/bin/python3
"""Define a class Square."""
class Square:
"""Represent a square."""
def __init__(self, size=0, position=(0, 0)):
"""Initialize a new square.
Args:
size (int): The size of the new square.
position (int, int): The position of the new square.
"""
self.size = size
self.position = position
@property
def size(self):
"""Get/set the current size of the square."""
return (self.__size)
@size.setter
def size(self, value):
if not isinstance(value, int):
raise TypeError("size must be an integer")
elif value < 0:
raise ValueError("size must be >= 0")
self.__size = value
@property
def position(self):
"""Get/set the current position of the square."""
return (self.__position)
@position.setter
def position(self, value):
if (not isinstance(value, tuple) or
len(value) != 2 or
not all(isinstance(num, int) for num in value) or
not all(num >= 0 for num in value)):
raise TypeError("position must be a tuple of 2 positive integers")
self.__position = value
def area(self):
"""Return the current area of the square."""
return (self.__size * self.__size)
def my_print(self):
"""Print the square with the # character."""
if self.__size == 0:
print("")
return
[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)]
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 ("")