I'm studying Python steadily at freeCodeCamp. In Previous article, this time we will challenge * Polygon Area Calculator *.
What is required is as follows
--Creating a Rectangle class --Creating a Square class
rect = shape_calculator.Rectangle(10, 5)
print(rect.get_area())
rect.set_height(3)
print(rect.get_perimeter())
print(rect)
print(rect.get_picture())
sq = shape_calculator.Square(9)
print(sq.get_area())
sq.set_side(4)
print(sq.get_diagonal())
print(sq)
print(sq.get_picture())
rect.set_height(8)
rect.set_width(16)
print(rect.get_amount_inside(sq))
50
26
Rectangle(width=10, height=3)
**********
**********
**********
81
5.656854249492381
Square(side=4)
****
****
****
****
8
class Rectangle:
def __init__(self, width, height):
self.width = width
self.height = height
def set_width(self, width):
self.width = width
def set_height(self, height):
self.height = height
def get_area(self):
return self.width * self.height
def get_perimeter(self):
return 2 * self.width + 2 * self.height
def get_diagonal(self):
return (self.width ** 2 + self.height ** 2) ** .5
def get_picture(self):
if(self.width >= 50 or self.height >= 50):
return "Too big for picture."
row = "*" * self.width
return "\n".join([row for idx in range(self.height)]) + "\n"
def get_amount_inside(self, shape):
return int(self.get_area() / shape.get_area())
def __str__(self):
return f"{self.__class__.__name__}(width={self.width}, height={self.height})"
The only thing I looked up was how about getting the class name? It was only the part. Are you growing?
The next issue is * Probability Calculator *.
Recommended Posts